PandasのDataFrameチュートリアル


1、DF作成
>>> import pandas as pd
>>> val = [[1,3,3,4],[5,6,7,8],[1,1,1,1],[2,3,2,3]]
>>> cols = ['A','B','C','D']
>>> indx= ['i1','i2','i3','i4']
>>> df = pd.DataFrame(val,columns=cols)              # DF  ,     
>>> print(df)
   A  B  C  D
0  1  3  3  4
1  5  6  7  8
2  1  1  1  1
3  2  3  2  3
>>> df = pd.DataFrame(val,columns=cols,index=indx)  #DF  ,    
>>> df
    A  B  C  D
i1  1  3  3  4
i2  5  6  7  8
i3  1  1  1  1
i4  2  3  2  3
>>> print(df.values)                                 #DF     
[[1 3 3 4]
 [5 6 7 8]
 [1 1 1 1]
 [2 3 2 3]]
>>> print(df[['A','B']].values)
[[1 3]
 [5 6]
 [1 1]
 [2 3]]
>>> df.columns                                        #  columns,  list
Index(['A', 'B', 'C', 'D'], dtype='object')
>>> df.columns.tolist()
['A', 'B', 'C', 'D']
>>> df.index
Index(['i1', 'i2', 'i3', 'i4'], dtype='object')       #    ,  list
>>> df.index.tolist()
['i1', 'i2', 'i3', 'i4']
>>>


2、DF遍歴
>>> for row in df.index:
...     print(df.loc[row][['A','B']])               #  ,    A,B 
...
A    1
B    3
Name: i1, dtype: int64
A    5
B    6
Name: i2, dtype: int64
A    1
B    1
Name: i3, dtype: int64
A    2
B    3
Name: i4, dtype: int64
>>>

3、DFクエリー、インデックス
>>> df
    A  B  C  D
i1  1  3  3  4
i2  5  6  7  8
i3  1  1  1  1
i4  2  3  2  3
>>> df.loc['i1','A']             #loc,          ,     ,             
1
>>> df.loc['i1':'i3','A':'C']    #  
    A  B  C
i1  1  3  3
i2  5  6  7
i3  1  1  1
>>>
>>> df.loc[['i1','i3'],['A','C']]  #   ,list
    A  C
i1  1  3
i3  1  1
>>> df
    A  B  C  D
i1  1  3  3  4
i2  5  6  7  8
i3  1  1  1  1
i4  2  3  2  3
>>> df.iloc[1,2]                   #.iloc             
7
>>> df.iloc[0:2,1:2]               #  
    B
i1  3
i2  6
>>> df.iloc[[0,1],[1,2]]           #   ,list
    B  C
i1  3  3
i2  6  7
>>>

4、DF修正
>>> df
    A  B  C  D
i1  1  3  3  4
i2  5  6  7  8
i3  1  1  1  1
i4  2  3  2  3
>>> df['A']=100                   #     
>>> df
      A  B  C  D
i1  100  3  3  4
i2  100  6  7  8
i3  100  1  1  1
i4  100  3  2  3
>>> df.loc['i1','A']=222           #     
>>> df
      A  B  C  D
i1  222  3  3  4
i2    5  6  7  8
i3    1  1  1  1
i4    2  3  2  3
>>>df.loc[df['A']==1,'B'] = 10    #          
    A  B  C  D
i1  1  10  3  4
i2  5  6  7  8
i3  1  10  1  1
i4  2  3  2  3

5、DF増加
>>> df
   A  B  C  D
0  1  3  3  4
1  5  6  7  8
2  1  1  1  1
3  2  3  2  3
>>> temp
   A  B  C  D
0  1  3  3  4
1  5  6  7  8
2  1  1  1  1
3  2  3  2  3
>>> result=df.append(temp,ignore_index=True)         #   ,    ,       
>>> result
   A  B  C  D
0  1  3  3  4
1  5  6  7  8
2  1  1  1  1
3  2  3  2  3
4  1  3  3  4
5  5  6  7  8
6  1  1  1  1
7  2  3  2  3
>>> df.loc[4]=[7,8,9,10]                             #    +   
>>> df
   A  B  C   D
0  1  3  3   4
1  5  6  7   8
2  1  1  1   1
3  2  3  2   3
4  7  8  9  10
>>>

pandasラーニング:seriesとdataframeをソートします.https://blog.csdn.net/u014662865/article/details/59058039
pandasのカラムの値に基づいて、DataFrameから行を選択します.https://www.cnblogs.com/to-creat/p/7724562.html