使用默认值将列添加到 Pandas DataFrame
使用默认值向 Pandas DataFrame 添加列的三种方法。
- 使用 pandas.DataFrame.assign(**kwargs)
- 使用 []运算符
- 使用 pandas.DataFrame.insert()
使用 Pandas.DataFrame.assign(**kwargs)
它将新列分配给 DataFrame,并将包含所有现有列的新对象返回给新对象。重新分配的现有列将被覆盖。
Syntax: Pandas.DataFrame.assign(**kwargs)
Parameters : **kwargsdict of {str: callable or Series}
Returns : 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 中。
Syntax: DataFrame.insert(loc, column, value, allow_duplicates=False)
Parameters
loc : int Insertion index. Must verify 0 <= loc <= len(columns).
column : str, number, or hashable object Label of the inserted column.
value : int, Series, or array-like
allow_duplicates : bool, optional
让我们通过例子来理解:
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
输出: