📌  相关文章
📜  使用默认值将列添加到 Pandas DataFrame

📅  最后修改于: 2022-05-13 01:54:55.830000             🧑  作者: Mango

使用默认值将列添加到 Pandas DataFrame

使用默认值向 Pandas DataFrame 添加列的三种方法。

  • 使用 pandas.DataFrame.assign(**kwargs)
  • 使用 []运算符
  • 使用 pandas.DataFrame.insert()

使用 Pandas.DataFrame.assign(**kwargs)

它将新列分配给 DataFrame,并将包含所有现有列的新对象返回给新对象。重新分配的现有列将被覆盖。

让我们通过例子来理解:

首先,创建一个简单的 DataFrame。

Python3
# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
  
                   "Address": ['Patna', 'Delhi', 'Coimbatore',
                               'Greater noida', 'Patna'],
  
                   "ID": [20123, 20124, 20145, 20146, 20147],
  
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
  
print("Original DataFrame :")
display(df)


Python3
new_df = df.assign(profit=[40000, 20000, 30000, 60000, 200000])
new_df


Python3
new_df = df.assign(profit='NAN')
new_df


Python3
# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                     
                   "Address": ['Patna', 'Delhi', 'Coimbatore', 
                               'Greater noida', 'Patna'],
                     
                   "ID": [20123, 20124, 20145, 20146, 20147],
                     
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
  
print("Original DataFrame :")
display(df)


Python3
df['loss'] = [40000, 20000, 30000, 60000, 200000]
df


Python3
df['loss'] = 'NAN'
df


Python3
# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                     
                   "Address": ['Patna', 'Delhi', 'Coimbatore', 
                               'Greater noida', 'Patna'],
                     
                   "ID": [20123, 20124, 20145, 20146, 20147],
                     
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
  
print("Original DataFrame :")
display(df)


Python3
df.insert(2, "expenditure", 4500, allow_duplicates=False)
df


输出:

添加一个新列:

Python3

new_df = df.assign(profit=[40000, 20000, 30000, 60000, 200000])
new_df

输出:

添加具有默认值的新列:

Python3

new_df = df.assign(profit='NAN')
new_df

输出:

使用 []运算符添加新列

我们可以使用 DataFrame 索引在 DataFrame 中创建一个新列并将其设置为默认值。

句法:

df[col_name]=value

让我们通过一个例子来理解:

Python3

# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                     
                   "Address": ['Patna', 'Delhi', 'Coimbatore', 
                               'Greater noida', 'Patna'],
                     
                   "ID": [20123, 20124, 20145, 20146, 20147],
                     
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
  
print("Original DataFrame :")
display(df)

输出:

在 Dataframe 中添加新列:

Python3

df['loss'] = [40000, 20000, 30000, 60000, 200000]
df

输出:

添加具有默认值的新列:

Python3

df['loss'] = 'NAN'
df

输出:

使用 pandas.DataFrame.insert()

在指定位置将新列添加到 DataFrame 中。

让我们通过例子来理解:

Python3

# importing pandas as pd
import pandas as pd
  
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                     
                   "Address": ['Patna', 'Delhi', 'Coimbatore', 
                               'Greater noida', 'Patna'],
                     
                   "ID": [20123, 20124, 20145, 20146, 20147],
                     
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
  
print("Original DataFrame :")
display(df)

输出:

添加具有默认值的新列:

Python3

df.insert(2, "expenditure", 4500, allow_duplicates=False)
df

输出: