简体   繁体   English

将 Pandas Dataframe 中的一列替换为另一列

[英]Replace one column with another in Pandas Dataframe

I'm working with a dataset that has more than 40 columns and I want to replace two columns data with another two columns of data and want to remove the existing one.我正在使用一个包含 40 多列的数据集,我想用另外两列数据替换两列数据,并想要删除现有的数据。

Example Dataset:示例数据集:

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

I want to replace the v5 column with pfv and s_51 with pfv_cat , by replace I mean overwrite`我想用pfvs_51 with pfv_cat 替换v5, by replace I mean覆盖`

Here's what I have tried:这是我尝试过的:

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

But it doesn't work, it's just stuck and no input.但它不起作用,它只是卡住并且没有输入。

You can just do你可以做

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

Input输入

     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

Output Output

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用另一列 Pandas DataFrame 替换一列中的值 - Replace values from one column with another column Pandas DataFrame Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe Pandas 将列的值替换为与另一个 Dataframe 的比较 - Pandas replace values of a column with comparison to another Dataframe 将数据从一个pandas数据帧替换为另一个 - Replace data from one pandas dataframe to another 用一列替换pandas数据框中的所有列 - Replace all columns in pandas dataframe with one column Pandas DataFrame)一列替换其他df - Pandas DataFrame) one column replace other df 将 pandas dataframe 列替换为另一个 dataframe 列 - Replace pandas dataframe column with another dataframe column with condition 替换 pandas dataframe 列中的元素(如果存在于另一个 dataframe 列中) - Replace an element in a pandas dataframe column if present in another dataframe column 将某些 pandas dataframe 列值从一列移到另一列,并将旧的 position 替换为 Nan - Move certain pandas dataframe column values from one column to another and replace old position with Nan 如果另一列不是 pandas DataFrame 中的 null,则将一列替换为另一列 - Replace a column with another column if another is not null in pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM