简体   繁体   English

如何根据列值在数据框单元格内附加字符串

[英]How to append strings inside dataframe cells based on column values

Given a dataframe:给定一个数据框:

import pandas as pd

df = pd.DataFrame(data= {'Col1': ['No', 'Yes', 'No', 'Maybe'], 'Col2': ['Yes', 'No', 'No', 'No'], 'Result': ''})

I want to populate Result with a list that may need to be appended based upon a column value.我想用一个列表填充Result ,该列表可能需要根据列值进行附加。 In this case, the parameters would be:在这种情况下,参数将是:

If the value is 'Yes' keep the current value of Result , if the value is 'Maybe' append 'Attention needed (insert column name)', if the value is 'No' append 'Failure (insert column name)'如果值为 'Yes' 保留Result的当前值,如果值为 'Maybe' append 'Attention needed (insert column name)',如果值为 'No' append 'Failure (insert column name)'

Desired result:想要的结果: 在此处输入图片说明

Not very pretty, but you could create a dict , then use stack , map and groupby with join aggregation:不是很漂亮,但您可以创建一个dict ,然后将stackmapgroupbyjoin聚合一起使用:

d = {'No': 'Failure', 'Maybe': 'Attention needed'}
s = df[['Col1', 'Col2']].stack().map(d).dropna()

df['Result'] = (s + ' ' + s.index.get_level_values(1)).groupby(level=0).agg(', '.join)

[out] [出去]

    Col1 Col2                               Result
0     No  Yes                         Failure Col1
1    Yes   No                         Failure Col2
2     No   No           Failure Col1, Failure Col2
3  Maybe   No  Attention needed Col1, Failure Col2

Try this one liner code using lambda function:使用lambda函数试试这个单行代码:

df['Result'] = df[['Col1','Col2']].apply(lambda x: 'Failure Col1' if (x[0]=='No' and x[1]=='Yes') else ('Failure Col2' if (x[1]=='No' and x[0]=='Yes') else ('Failure Col1, Failure Col2' if (x[0]=='No' and x[1]=='No') else("Attention needed Col1, Failure Col2" if (x[0]=='Maybe' and x[1]=='No') else None))), axis=1)

Output:输出:


   Col1     Col2    Result
0   No      Yes     Failure Col1
1   Yes     No      Failure Col2
2   No      No      Failure Col1, Failure Col2
3   Maybe   No      Attention needed Col1, Failure Col2

您可以首先将结果列构造为一个 numpy 数组,同时遍历数据框列并检查值,然后您可以添加新的结果列并删除旧的结果列。

Construct a dictionary to replace values in df and Using * and + to construct a series of appropriate message strings and finally join them and assign to df.Result构造一个字典来替换df值并使用*+构造一系列合适的消息字符串,最后将它们连接起来并赋值给df.Result

d = {'Yes': '', 'No': 'Failure ', 'Maybe': 'Attention needed '}
df1 = df[['Col1', 'Col2']]
df['Result'] = ((df1.replace(d) 
                + df1.ne('Yes').values * df1.columns.values).agg(','.join, axis=1)
                                                            .str.strip(','))

Or或者

df['Result'] = ((df1.replace(d) 
                + df1.ne('Yes').values * (df1.columns+',').values).sum(1)
                                                                  .str.strip(','))

Out[267]:
    Col1 Col2                              Result
0     No  Yes                        Failure Col1
1    Yes   No                        Failure Col2
2     No   No           Failure Col1,Failure Col2
3  Maybe   No  Attention needed Col1,Failure Col2

Here the detail这里的细节

df1.replace(d) + df1.ne('Yes').values * df1.columns.values

Out[268]:
                    Col1          Col2
0           Failure Col1
1                         Failure Col2
2           Failure Col1  Failure Col2
3  Attention needed Col1  Failure Col2

((df1.replace(d) + df1.ne('Yes').values * df1.columns.values).agg(','.join, axis=1)
                                                             .str.strip(','))

Out[269]:
0                          Failure Col1
1                          Failure Col2
2             Failure Col1,Failure Col2
3    Attention needed Col1,Failure Col2
dtype: object

暂无
暂无

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

相关问题 如何使用基于条件的值将 append 列到 dataframe - How to append a column to a dataframe with values based on condition 如何将基于其他列值的列附加到pandas数据框 - How to append columns based on other column values to pandas dataframe How to filter the rows of a dataframe based on the presence of the column values in a separate dataframe and append columns from the second dataframe - How to filter the rows of a dataframe based on the presence of the column values in a separate dataframe and append columns from the second dataframe pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe append 列的值基于 dataframe 中另一列的值 - append column with values based on values of another column in the dataframe Append 到 Dataframe 的列 1 基于 Dataframe 中的匹配列值 2 - Append Columns to Dataframe 1 Based on Matching Column Values in Dataframe 2 如何基于另一列将 append 数据转换为 dataframe? - How to append data to dataframe based on another column? 如何将 Append 列到字典中的 Dataframe - How to Append a Column to a Dataframe which is inside a Dictionary 如何将一个数据帧的列值附加到另一个数据帧的列 - How to append column values of one dataframe to column of another dataframe 如何根据 Python 中的列值将 pandas dataframe 单元格设置为等于? - How do I set pandas dataframe cells equal to based on column values in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM