用Pandas Dataframe中的一列替换另一列

问题描述 投票:0回答:1

我正在使用具有40多个列的数据集,我想用另外两列数据替换两列数据,并希望删除现有的一列数据。

示例数据集:

v_4        v5             s_5     vt_5     ex_5          pfv           pfv_cat
0-50      StoreSale     Clothes   8-Apr   above 100   FatimaStore       Shoes
0-50      StoreSale     Clothes   8-Apr   0-50        DiscountWorld     Clothes
51-100    CleanShop     Clothes   4-Dec   51-100      BetterUncle       Shoes

我想将v5列替换为pfvs_51 with pfv_cat, by replace I meanoverwrite`

这是我尝试过的:

df.replace(df['v_5'].tolist(), df['pfv'].tolist())
df.replace(df['s_5'].tolist(), df['pfv_cat'].tolist())

但是它不起作用,只是卡住了,没有输入。

python python-3.x pandas dataframe
1个回答
0
投票

您可以做

df['v_5']=df['pfv']
df['s_5']=df['pfv_cat']
print(df)

输入

     v_4          v5    s_5         vt_5    ex_5             pfv        pfv_cat
0   0-50    StoreSale   Clothes     8-Apr   above100    FatimaStore     Shoes
1   0-50    StoreSale   Clothes     8-Apr   0-50        DiscountWorld   Clothes
2   51-100  CleanShop   Clothes     4-Dec   51-100      BetterUncle     Shoes

输出

v_4     v5  s_5     vt_5    ex_5    pfv     pfv_cat     v_5
0   0-50    StoreSale   Shoes   8-Apr   above100    FatimaStore     Shoes   FatimaStore
1   0-50    StoreSale   Clothes     8-Apr   0-50    DiscountWorld   Clothes     DiscountWorld
2   51-100  CleanShop   Shoes   4-Dec   51-100  BetterUncle     Shoes   BetterUncle
© www.soinside.com 2019 - 2024. All rights reserved.