Pandas 04_DataFrame library index data structure and select _

import pandas as pd

= {the Data
"name": [ "Don Ho", "Wang", "Pharaoh", "Zhao Three", "John Doe"],
"Sex": [ "male", "female", "male" , "woman", "man"],
"year": [37,22,15,18,33],
"City": [ "Chengdu", "Beijing", "Shanghai", "Chengdu", "Shenzhen" ]
}

#DataFrame sort sort_values (by = "Field i.e. Key")
#eg:
# = pd.DataFrame DF1 (Data)
# Print (DF1)
# Print ( "_______________________-")
# DF2 = df1.sort_values (by = "year ") # just sort of, but index order has not changed,
# Print (DF2)
# Print (" _______________________- ")
# # we delete the original index order on the line
# df3 = df2.reset_index () # rebuild the index, do not delete original index
# Print (DF3)
# DF4 = df2.reset_index (drop = True) # rebuild the index, delete the original index
# print (df4)

# Data selection
#Series data selection and list of almost similar, do not say, following about DataFrame, he is more complicated

df5=pd.DataFrame(data)
# print(df5)

Select # column, there are two ways: df5 [ "name"] or df5.name, can be obtained, a return data columns Series
# Print (DF5 [ "name"])
# Print (df5.city)
# select multiple column
# Print (DF5 [[ "name", "City"]])
# column can not be used to select a slice, a slice row is operated, but when the data operation you can select column sections

Select row #, loc, iloc, ix like
# Print (DF5)
# = DF6 df5.set_index ( "name") will be # a column index tab to column
# Print (DF6)
# Print (df6.loc [ "Don Ho "]) to select by index tab # LOC [" If there is to be an index tab, the other being given "]
# Print (" ______________ ")
# Print (df6.loc [[" Don ho "," Wang "]]) # by a plurality of index tab to select
# supra, iloc [1] this is an index to select a position.

# Advanced mode, select the rows and columns ix
# Print (DF5)
# DF6 = df5.set_index ( "name")
# DF6 = df5.ix [:,:] # Select all ranks, ix [row, column]
# Print ( DF6)
# DF6 = df5.set_index ( "name") set up to use the following, no you can only use index number
# df7 = df6.ix [[ "Don ho", "John Doe"] ,:]
# = df5.ix DF8 [:, [ "name", "Sex"]]
# Print (DF8)
# DF9 = df5.ix [0]
# Print (DF9)

###### bool selected conditions, corresponding to a selected incorrect value, bool obtain a first layer, a second layer to obtain the relevant data
# Print (DF5)
# DF5 DF10 = [(DF5 [ "City"] == "Chengdu") & (df5 [ "sex "] == " M")]
# = DF5 DF10 [(DF5 [ "City"] == "Chengdu") & (df5 [ "sex "] == " M" )] corresponding to conditions # screened
# df11 = df5 [(df5 [ "year"]> 20) & (df5 [ "sex"] == " M") & (df5 [ "city "] == " Chengdu" )] is equivalent to multiple criteria to filter the #
# Print (DF11)
# Print (df11.sort_values ( "year"))


# Operation rows and columns: the add, delete, change, search (select front learned)
# Print (DF5)
"" "
name Sex year City
0 Tang Hao male 37 Chengdu
1 Wang Female 22 Beijing
2 Pharaoh Male 15 Shanghai
3 Zhao three women Chengdu 18
4 33 John Doe male Shenzhen
"" "
# just above the operating table inside the bar

# Add lines by df5.append (typically data word)
# NewData = { "name": "red", "sex": "female", "year": 20, "city": " the river"}
# DF6 = df5.append (newdata, ignore_index = True ) # generate a new data, the original data is unchanged, df5 constant
## in order to change the original data, it = df5.append DF5 (...)
# Print (DF6)
# # Add row
# df5 [ "love"] = " puppy" # assign a name for the column that does not exist on the new one, but all the data is the same,
# Print (DF5)
# DF5 [ "wwww "] = [" abc ", " def "," abbbb "," xxxxxx "," dfdfsfds "] # but not too much, just so much the error will not
# print (df5)

# Delete
# df8 = df5.drop (1) # original data unchanged, that df5 no change, drop (position) Delete Row
# Print (DF8)
# DF9 = df5.drop ( "year", Axis = 1) #AXIS 1 to find the x-axis, y-axis to find 0. Remove Columns
# print (df9)

# Modify, edit here talking about the row and column index modifications and column labels, modify the value has not been mentioned

Df5.ix DF10 = # [[1, 2,4] ,:]
# Print (DF10)
# = df10.rename DF11 (index = {. 4:. 3}, {Columns = "City": "the CITY"}, InPlace = False) #True is modified on the original data
# Print (DF11)
# DF11 [ "name"] [. 1] = "Survival Song"
# Print (DF11)

Guess you like

Origin www.cnblogs.com/yiyea/p/11441795.html