Pandas の既存の DataFrame に新しい列を追加する
既存の DataFrame に新しい列を追加することは、 パンダ 。これにより、追加情報でデータを強化し、さらなる分析と操作を容易にすることができます。この記事では、単純な割り当て、 insert()> メソッド、 assign()> 方法。 Pandas の既存の DataFrame に新しい列を追加する方法について説明します。
パンダデータフレームとは何ですか?
あ パンダのデータフレーム は、ラベル付きの軸 (行と列) を備えた、サイズ変更可能で、潜在的に異種混合の表形式のデータ構造です。これは Python データ サイエンス エコシステムの基本的なデータ構造であり、表形式データを操作するための強力な方法を提供します。
Pandas DataFrame の主な機能をいくつか示します。
- データ表現: データを行と列を含むテーブル形式で保存します。
- 異種データ型: 異なる列に異なるデータ型を保持できます (整数、浮動小数点数、文字列、ブール値など)。
- ラベリング: 各行と列にはラベル (インデックスと列名) があります。
- 可変: データの操作と変更を許可します。
- 強力な操作: データ分析、操作、探索のためのさまざまな機能と方法を提供します。
- 拡張可能: ライブラリやユーザー定義関数を使用して追加機能をカスタマイズおよび拡張できます。
Pandas の既存のデータフレームに新しい列を追加するには、複数の方法があります。 パイソン :
- サンプル データフレームの作成
- を使用することで データフレーム.挿入() 方法
- を使用することで データフレーム.assign() 方法
- 辞書の使用
- リストの使用
- 使用する 。場所()
- 既存のデータフレームに複数の列を追加する
サンプル データフレームの作成
ここではサンプル データフレームを作成しています。
Python3
import> pandas as pd> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ]}> df> => pd.DataFrame(data)> print> (df)> |
出力:
Name Height Qualification 0 Jai 5.1 Msc 1 Princi 6.2 MA 2 Gaurav 5.1 Msc 3 Anuj 5.2 Msc
リストの長さはインデックス列の長さと一致する必要があり、そうでない場合はエラーが表示されることに注意してください。
DataFrame.insert() を使用して既存の Datframe に新しい列を追加する
最後だけでなく、好きな位置に列を自由に追加できます。また、列値を挿入するためのさまざまなオプションも提供します。
Python3
import> pandas as pd> # Define a dictionary containing Students data> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data)> # Using DataFrame.insert() to add a column> df.insert(> 2> ,> 'Age'> , [> 21> ,> 23> ,> 24> ,> 21> ],> True> )> # Observe the result> print> (df)> |
出力:
Name Height Age Qualification 0 Jai 5.1 21 Msc 1 Princi 6.2 23 MA 2 Gaurav 5.1 24 Msc 3 Anuj 5.2 21 Msc
Dataframe.assign() を使用して Pandas DataFrame に列を追加する
このメソッドは、古いデータフレームに新しい列を追加した新しいデータフレームを作成します。
Python3
import> pandas as pd> # Define a dictionary containing Students data> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ]}> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data)> # Using 'Address' as the column name and equating it to the list> df2> => df.assign(address> => [> 'Delhi'> ,> 'Bangalore'> ,> 'Chennai'> ,> 'Patna'> ])> print> (df2)> |
出力:
Name Height Qualification address 0 Jai 5.1 Msc Delhi 1 Princi 6.2 MA Bangalore 2 Gaurav 5.1 Msc Chennai 3 Anuj 5.2 Msc Patna
パンダは辞書を使用して DataFrame に列を追加します
を使用できます Python辞書 pandas DataFrame に新しい列を追加します。既存の列をキー値として使用すると、それぞれの値が新しい列の値になります。
Python3
# Import pandas package> import> pandas as pd> # Define a dictionary containing Students data> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ]}> # Define a dictionary with key values of> # an existing column and their respective> # value pairs as the # values for our new column.> address> => {> 'Delhi'> :> 'Jai'> ,> 'Bangalore'> :> 'Princi'> ,> > 'Patna'> :> 'Gaurav'> ,> 'Chennai'> :> 'Anuj'> }> # Convert the dictionary into DataFrame> df> => pd.DataFrame(data)> # Provide 'Address' as the column name> df[> 'Address'> ]> => address> # Observe the output> print> (df)> |
出力:
Name Height Qualification Address 0 Jai 5.1 Msc Delhi 1 Princi 6.2 MA Bangalore 2 Gaurav 5.1 Msc Chennai 3 Anuj 5.2 Msc Patna
List を使用して Pandas DataFrame に新しい列を追加する
この例では、 パンダはリストから新しい列を追加します 辞書とリストを使用して、既存の Pandas DataFrame にアドレス指定します。
Python3
# Declare a list that is to be converted into a column> address> => [> 'Delhi'> ,> 'Bangalore'> ,> 'Chennai'> ,> 'Patna'> ]> # Using 'Address' as the column name> # and equating it to the list> df[> 'Address'> ]> => address> print> (df)> |
出力:
Name Height Qualification Address 0 Jai 5.1 Msc Delhi 1 Princi 6.2 MA Bangalore 2 Gaurav 5.1 Msc Chennai 3 Anuj 5.2 Msc Patna
Dataframe.loc() を使用して既存の Pandas DataFrame に新しい列を追加する
この例では、という名前の Pandas DataFrame を作成します。 df> Name、Height、Qualification の列があり、 loc> 属性。
Python3
import> pandas as pd> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ]}> df> => pd.DataFrame(data)> # Create the list of new column values> address> => [> 'Delhi'> ,> 'Bangalore'> ,> 'Chennai'> ,> 'Patna'> ]> # Add the new column using loc> df.loc[:,> 'Address'> ]> => address> print> (df)> |
出力:
Name Height Qualification Address 0 Jai 5.1 Msc Delhi 1 Princi 6.2 MA Bangalore 2 Gaurav 5.1 Msc Chennai 3 Anuj 5.2 Msc Patna
既存のデータフレームに複数の列を追加する
この例では、既存の Pandas DataFrame を拡張します。 df> Age と State という 2 つの新しい列があり、それぞれのデータ リストが使用されます。
Python3
import> pandas as pd> data> => {> 'Name'> : [> 'Jai'> ,> 'Princi'> ,> 'Gaurav'> ,> 'Anuj'> ],> > 'Height'> : [> 5.1> ,> 6.2> ,> 5.1> ,> 5.2> ],> > 'Qualification'> : [> 'Msc'> ,> 'MA'> ,> 'Msc'> ,> 'Msc'> ],> > 'Address'> : [> 'Delhi'> ,> 'Bangalore'> ,> 'Chennai'> ,> 'Patna'> ]}> df> => pd.DataFrame(data)> # Define new data for additional columns> age> => [> 22> ,> 25> ,> 23> ,> 24> ]> state> => [> 'NCT'> ,> 'Karnataka'> ,> 'Tamil Nadu'> ,> 'Bihar'> ]> # Add multiple columns using dictionary assignment> new_data> => {> 'Age'> : age,> 'State'> : state }> df> => df.assign(> *> *> new_data)> print> (df)> |
出力:
Name Height Qualification Address Age State 0 Jai 5.1 Msc Delhi 22 NCT 1 Princi 6.2 MA Bangalore 25 Karnataka 2 Gaurav 5.1 Msc Chennai 23 Tamil Nadu 3 Anuj 5.2 Msc Patna 24 Bihar
結論
Pandas でのデータの探索と操作には、DataFrame に新しい列を追加する方法を理解することが不可欠です。適切な方法の選択は、特定のコンテキストと望ましい結果によって異なります。これらのテクニックをマスターすると、データを効果的に操作、分析し、そこから貴重な洞察を得ることができます。