このPythonチュートリアルでは、pandas DataFrameオブジェクトに1行または複数行を追加または挿入する上位5つの方法について説明します。
では、さっそく議論を始めましょう。
Pandasのデータフレームに行を追加するメソッド
まずは手始めにサンプルのpandas DataFrameオブジェクトを作成し、以下のメソッドを使って1行または複数行を追加していきます。
# Import pandas Python moduleimport pandas as pd
# Create a sample pandas DataFrame objectdf = pd.DataFrame({'RegNo': [111, 112, 113, 114, 115],
'Name': ['Gautam', 'Tanya', 'Rashmi', 'Kirti', 'Ravi'],
'CGPA': [8.85, 9.03, 7.85, 8.85, 9.45],
'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
'City': ['Jalandhar','Ranchi','Patna','Patiala','Rajgir']})
# Print the created pandas DataFrameprint('Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 113 Rashmi 7.85 IT Patna3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir |
この記事もチェック:Pandasのreplaceメソッドを使って複数の値を置換する方法
方法その1
pandas Series オブジェクトを既存の pandas DataFrame オブジェクトに行として追加します。
# Create a pandas Series object with all the column values passed as a Python lists_row = pd.Series([116,'Sanjay',8.15,'ECE','Biharsharif'], index=df.columns)
# Append the above pandas Series object as a row to the existing pandas DataFrame# Using the DataFrame.append() functiondf = df.append(s_row,ignore_index=True)
# Print the modified pandas DataFrame object after addition of a rowprint('Modified Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Modified Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 113 Rashmi 7.85 IT Patna3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir5 116 Sanjay 8.15 ECE Biharsharif |
方法その2
Python辞書を既存のpandas DataFrameオブジェクトに行として追加します。
# Create a Python dictionary object with all the column valuesd_row = {'RegNo':117,'Name':"Sarthak",'CGPA':8.88,'Dept':"ECE",'City':"Allahabad"}
# Append the above Python dictionary object as a row to the existing pandas DataFrame# Using the DataFrame.append() functiondf = df.append(d_row,ignore_index=True)
# Print the modified pandas DataFrame object after addition of a rowprint('Modified Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Modified Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 113 Rashmi 7.85 IT Patna3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir5 116 Sanjay 8.15 ECE Biharsharif6 117 Sarthak 8.88 ECE Allahabad |
NOTE: Python辞書やpandasシリーズを渡す際には、 DataFrame.append() 関数の ignore_index パラメータを True に設定する必要があります。
方法その3
Pythonのリストオブジェクトを既存のpandas DataFrameオブジェクトに DataFrame.loc[] メソッドで行として追加します。
# Create a Python list object with all the column valuesl_row = [118,"Kanika",7.88,"EE","Varanasi"]
# Append the above Python list object as a row to the existing pandas DataFrame# Using the DataFrame.loc[]df.loc[7] = l_row
# Print the modified pandas DataFrame object after addition of a rowprint('Modified Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Modified Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 113 Rashmi 7.85 IT Patna3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir5 116 Sanjay 8.15 ECE Biharsharif6 117 Sarthak 8.88 ECE Allahabad7 118 Kanika 7.88 EE Varanasi |
方法その4
DataFrame.append()` 関数を用いて、ある pandas DataFrame オブジェクトの行を別の pandas DataFrame オブジェクトに追加します。
# Create a new pandas DataFrame objectdf2 = pd.DataFrame({'RegNo': [119, 120, 121],
'Name': ['Gaurav', 'Thaman', 'Radha'],
'CGPA': [8.85, 9.03, 7.85],
'Dept': ['ECE', 'ICE', 'IT'],
'City': ['Jalandhar','Ranchi','Patna']})
# Print the newly created pandas DataFrame objectprint('New pandas DataFrame:)
print(df2)
# Append the rows of the above pandas DataFrame to the existing pandas DataFrame# Using the DataFrame.append()df = df.append(df2,ignore_index=True)
# Print the modified pandas DataFrame object after addition of rowsprint(')
print(df)
|
結果は以下の通りです。
New pandas DataFrame: RegNo Name CGPA Dept City
0 119 Gaurav 8.85 ECE Jalandhar1 120 Thaman 9.03 ICE Ranchi2 121 Radha 7.85 IT PatnaModified Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 113 Rashmi 7.85 IT Patna3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir5 116 Sanjay 8.15 ECE Biharsharif6 116 Sanjay 8.15 ECE Biharsharif7 118 Kanika 7.88 EE Varanasi8 119 Gaurav 8.85 ECE Jalandhar9 120 Thaman 9.03 ICE Ranchi10 121 Radha 7.85 IT Patna |
メソッド#5
既存のpandas DataFrameオブジェクトに、 DataFrame.iloc[] メソッドを用いて特定のインデックス位置に行を追加します。
# Create a Python list object with all the column valuesi_row = [122,"Zahir",6.88,"ME","Kolkata"]
# Append the above Python list object as a row to the existing pandas DataFrame# At index 2 using the DataFrame.iloc[]df.iloc[2] = i_row
# Print the modified pandas DataFrame object after addition of a rowprint('Modified Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
結果は以下の通りです。
Modified Sample pandas DataFrame: RegNo Name CGPA Dept City
0 111 Gautam 8.85 ECE Jalandhar1 112 Tanya 9.03 ICE Ranchi2 122 Zahir 6.88 ME Kolkata3 114 Kirti 8.85 CSE Patiala4 115 Ravi 9.45 CHE Rajgir5 116 Sanjay 8.15 ECE Biharsharif6 116 Sanjay 8.15 ECE Biharsharif7 118 Kanika 7.88 EE Varanasi8 119 Gaurav 8.85 ECE Jalandhar9 120 Thaman 9.03 ICE Ranchi10 121 Radha 7.85 IT Patna |
NOTE: DataFrame.iloc[] メソッドを使用する際には、そのインデックスの位置にある既存の行を新しい行に置き換えるので注意が必要です。
まとめ
この記事では、既存のpandas DataFrameオブジェクトに1行または複数行を追加または挿入するための上位5つのメソッドを学びました。
このチュートリアルで説明したことをよく理解し、自分のデータ分析プロジェクトでこれらのメソッドを使用する準備ができていることを望みます。
お読みいただきありがとうございました。
これからもPythonプログラミングの学習リソースを提供していきますので、よろしくお願いします。