简体   繁体   English

如何在大熊猫不相等的值上将数据从一列移动到另一列?

[英]How do i move data from one column to another on values that aren't equal in pandas?

I have two columns in one data frame 'first name' and 'preferred name'. 我在一个数据框中有两列“名字”和“首选名称”。 If the preferred name is different to first name, I want to move that value to first name. 如果首选名称与名字不同,我想将该值移动到名字。 eg: 例如:

   First Name  Preferred Name  
1  David       Dave       
2  John        John        
3  Sarah       Sarah
4  Elizabeth   Liz



   First Name  Preferred Name  
1  Dave        Dave       
2  John        John        
3  Sarah       Sarah
4  Liz         Liz  

slightly impatient, so i did it the 'ugly' way. 有点不耐烦,所以我做了“丑陋”的方式。 queried the data for indices where names are different, and then added them on those values 查询名称不同的索引的数据,然后将它们添加到这些值上

name_index = df[df['first name'] != df['preferred name']]
df.ix[name_index,'first name'] = df.ix[name_index,'preferred name']

The simpliest is only assign as pointed EdChum : 最简单的只是指定为EdChum

df['first name'] = df['preferred name']
print (df)
  first name preferred name
1       Dave           Dave
2       John           John
3      Sarah          Sarah
4        Liz            Liz

You can first create boolean mask where difference and then add new values by loc or by mask : 您可以先创建差异的boolean掩码,然后按locmask添加新值:

mask = df['first name'] != df['preferred name']
df.loc[mask, 'first name'] = df['preferred name']
print (df)
  first name preferred name
1       Dave           Dave
2       John           John
3      Sarah          Sarah
4        Liz            Liz

Another solution: 另一种方案:

mask = df['first name'] != df['preferred name']
df['first name'] = df['first name'].mask(mask,df['preferred name'])
print (df)
  first name preferred name
1       Dave           Dave
2       John           John
3      Sarah          Sarah
4        Liz            Liz

暂无
暂无

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

相关问题 如何基于与熊猫匹配的列值将数据从一个数据帧移动到另一个数据帧? - How to move data from one dataframe to another based on matching column values with pandas? 如何将一些单元格值从 Pandas DF 中的 2 列移动到另一个新列? - How do I move some cell values from 2 columns in Pandas DF to another new column? 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 如何将数据从一列移动到另一列 Python DataFrame - How do I move data from one column to another in Python DataFrame pandas:如何根据列值在一个数据帧中从另一个数据帧中 append 行? - pandas: How can I append rows in one data frame from another based on column values? 如何将数据从一列移动到另一列? - How to move data from one column to another? 如何检查 pandas dataframe 列中的所有值是否相等? - How do I check if all values in a column of a pandas dataframe are equal? 在 pandas 中,如何根据一列中的唯一值创建列,然后根据另一列中的值填充它? - In pandas, how do I create columns out of unique values in one column, and then fill it based on values in another column? 如何将熊猫列中的值从某个位置移动到另一个位置? - how to move values in a pandas column from some position to another? 如何将特定数据从一列移动到 Pandas 上的新列? - How to move specific data from one column to a new column on Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM