- In [37]: import pandas as pd
- ...: import numpy as np
- ...: def pd_column_modify_value(df,col,dic):
- ...: """
- ...: 对某列的某个值进行统一修改。
- ...: 对df数据的col列按照字典dic进行对应替换值
- ...: """
- ...: df["name_"]=[""]*len(df)
- ...: df["name_"]=df[col].map(dic)
- ...: return df
- In [38]: df = pd.DataFrame(np.random.randint(1,10,size=(5,2)),columns=['A','B'])
- ...: df
- Out[38]:
- A B
- 0 9 5
- 1 4 1
- 2 3 5
- 3 6 7
- 4 9 4
- In [39]: nw = {5:0,2:"?"}
- ...: pd_column_modify_value(df,"B",nw)
- Out[39]:
- A B name_
- 0 9 5 0
- 1 4 1 NaN
- 2 3 5 0
- 3 6 7 NaN
- 4 9 4 NaN
还可以输入200个字
蝈蝈派【海南省教改项目(Hnjg2022-80)支持】 网站版权所有
Python remained the copyright of our website