Python Pandsは列/行を選択し、追加、削除します。


一、列操作
1.1列の選択

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print (df ['one'])
#           ,          
#    index     ,         ,       
実行結果:
a.    1.0
b    2.0
c    3.0
d    NaN
Name:one,dtype:float 64
1.2列を増やす

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

# Adding a new column to an existing DataFrame object with column label by passing new series

print ("Adding a new column by passing as Series:")
df['three']=pd.Series([10,30,20],index=['a','c','b'])
print(df)
#         ,   index              (                    )

print ("Adding a new column using the existing columns in DataFrame:")
df['four']=df['one']+df['two']+df['three']
print(df)
#       ,                   ,   NaN         ,   NaN

実行結果:
Adding a new column by passing as Series:
   one  two  three
a.  1.0    1   10.0
b  2.0    2   20.0.
c  3.0    3   30.0
d  NaN    4    NaN
Adding a new column using the existing columns in DataFrame:
   one  two  three  four
a.  1.0    1   10.0  12.0
b  2.0    2   20.0.  24.0
c  3.0    3   30.0  36.0
d  NaN    4    NaN   NaN
1.3列(delとpop関数)を削除する

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
  'three' : pd.Series([10,20,30], index=['a','b','c'])}

df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)

#    del   
print ("Deleting the first column using DEL function:")
del(df['one'])
print(df)

#    pop   
print ("Deleting another column using POP function:")
df_2=df.pop('two') #     pop     dataframe
print(df_2)
print(df)
実行結果:
Our data frame is:
   one  two  three
a.  1.0    1   10.0
b  2.0    2   20.0.
c  3.0    3   30.0
d  NaN    4    NaN
Deleting the first column using DEL function:
   two  three
a.    1   10.0
b    2   20.0.
c    3   30.0
d    4    NaN
Deleting another column using POP function:
   three
a.   10.0
b   20.0.
c   30.0
d    NaN
POP column:
a.    1
b    2
c    3
d    4
Name:two,dtype:int 64
二、行操作
2.1行の選択
2.1.1 labelで行を選択する(loc関数)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.loc['b']) #       ,            ,       index       
実行結果:
one    2.0
two    2.0
Name:b,dtype:float 64
2.1.2シリアル番号で行を選択する(iloc関数)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.iloc[2]) #    2       3     
実行結果:
one    3.0
two    3.0
Name:c,dtype:float 64
2.1.3シリアル番号で行スライスを選択する

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df[2:4]) #       3     4  ,  Python     ,     ,      

実行結果:
   one  two
c  3.0    3
d  NaN    4
2.2行を増やす(apped関数)

#    append   
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)
print(df) #            dataframe         ,    index    0 1
print(df.loc[0]) #        index   0
実行結果:
   a.  b
0  1  2
1  3  4
0  5  6
1  7  8
   a.  b
0  1  2
0  5  6
2.3行の削除(drop関数)

#    drop   
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)

df = df.drop(0) #           0,        2  
print(df)

実行結果:
   a.  b
1  3  4
1  7  8
ここでは、Python Pandsについて列/行を選択し、追加し、削除操作の記事をここに紹介します。Python Pandsの行列に関連して、削除内容を追加します。以前の文章を検索したり、下記の関連記事を見たりしてください。これからも私達を応援してください。