pandas 根据另一列的值从一列复制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29991862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:17:58  来源:igfitidea点击:

Copy value from one column based on the value of another column

pythonpython-2.7pandas

提问by Nicholas Tulach

I'm trying to fill values in one column from two other columns based on the values in a fourth column.

我正在尝试根据第四列中的值填充其他两列中的一列中的值。

I have a pandas dataframe with four columns: A, B, C, D

我有一个包含四列的 Pandas 数据框:A、B、C、D

df_copy = df.copy()
for i, row in df.iterrows():
    if 'Test' in row.D:
        df_copy.loc[i, 'A'] = row.B
    elif 'Other' in row.D:
        df_copy.loc[i, 'A'] = row.C

This works, but is very slow. Is there a more efficient way?

这有效,但速度很慢。有没有更有效的方法?

回答by joris

You can use 'boolean indexing' for this instead of iterating over all rows:

您可以为此使用“布尔索引”,而不是遍历所有行:

df_copy.loc[df['D']=='Test', 'A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']

If you know that column D only consists of these two values, it can even shorter:

如果您知道 D 列仅包含这两个值,则它甚至可以更短:

df_copy['A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']

If you want to have the same as the inoperator to test if that substring is in the column, you can do:

如果你想和in操作符一样来测试该子串是否在列中,你可以这样做:

df['D'].str.contains('Other')

to become the boolean values instead of the df['D']=='Other'

成为布尔值而不是 df['D']=='Other'