A Data box is a two-dimensional data structure, similar to a table in SQL. Data boxes can be constructed using dictionaries, arrays, lists, and sequences.
1. If the dictionary data box is created, the column name is the key name:
d = {‘one‘:pd.Series([1,2,3],index= [‘a‘,‘b‘,‘c‘]), ‘two‘:pd.Series([1,2,3,4],index=[‘a‘,‘b‘,‘c‘,‘d‘])}print(pd.DataFrame(d))
2. List creation data box:
d = pd.DataFrame([[1,2,3,4],[5,6,7,8],[10,20,30,40],[50,60,70,80]],columns=[‘V1‘,‘V2‘,‘V3‘,‘V4‘])print(d)
3. Column Selection, deletion, and addition:
DF = PD. dataframe ([[,], [,], [,], [50, 60,], columns = ['v1 ', 'v2 ', 'v3 ', 'v4']) print (DF, '\ n ') DF ['v5 '] = DF ['v1'] * DF ['v2'] # multiply the first column by the second column; print ('multiply the first column by the second column :') print (DF ['v5 '],' \ n') dF ['flag'] = DF ['v2']> 20 # Add a column; the value of the logical result is assigned to flagprint ('Add a column: ') print (DF,' \ n') del DF ['flag'] # delete column V5 = DF. pop ('v5 ') # delete column print ('delete column:') print (DF, '\ n', V5,' \ n') DF. insert (2, 'v6 ', DF ['v1']) #2 indicates the insert position, and V6 indicates the column name, DF ['v1 '] is the inserted value print ('insert column:') print (DF, '\ n') print (' * 50)
4. General selection methods:
Operation Method |
Method |
Result |
Select a column |
Def [col] |
Sequence |
Select a row using column tags |
DF. Loc [col] |
Sequence |
Select a row by location |
DF. icol [2] |
Sequence |
Line Cutting |
DF [5: 10] |
Data box |
Read one row of data |
DF. Loc [0,:] DF. iloc [0,:] DF. IX [0,:] |
|
Pandas dataframe data frame