数据准备
import pandas as pd
df = pd.DataFrame([['ABC'],
['FJZ'],
['FOC']
],columns=['Site'])
df
在平时开发中,添加新数据列涉及到以下三种常用方法:
1. 直接赋值
语法: df[新列名] = 值
import pandas as pd
import numpy as np
df = pd.DataFrame([['ABC'],
['FJZ'],
['FOC']
],columns=['Site'])
# 添加空值数据列
# 添加'Level','Remark'列,将两列的值设置为空(keep blank)
df['Level'] = np.nan
df['Remark'] = np.nan
2.reindex()函数
语法: df.reindex(columns=[原来所有的列名,新增列名],fill_value=值)
import pandas as pd
df = pd.DataFrame([['ABC'],
['FJZ'],
['FOC']
],columns=['Site'])
# 添加新列'Quantity'和'Product_number',并将其值设置为0
df = df.reindex(columns = ['Site', 'Quantity', 'Product_number'], fill_value=0)
注意: 不加fill_value参数,默认值为nan
3.loc()函数
语法: df.loc[:,新列名] = 值
import pandas as pd
df = pd.DataFrame([['ABC'],
['FJZ'],
['FOC']
],columns=['Site'])
# 添加新列'Description',并将其值设置为'Good'
df.loc[:,'Description'] = 'Good'
练习案例:增加一个自增id列
import pandas as pd
df = pd.DataFrame([['L123',1,2,3],
['L456',4,5,6],
['L437',7,8,9],
['L112',10,11,12],
['L211',13,14,15],
['L985',16,17,18]
],columns=['Material','W1|6/22','W2|6/23','W3|6/24'])
df.index = df.index + 1
df = df.reset_index()
df = df.rename(columns = {'index':'id'})
df
df(增加自增id列)