简体   繁体   English

如何在新列中添加具有特定条件的列的字符串值

[英]How to add string values of columns with a specific condition in a new column

So I have a dataframe in which there are a couple of columns and a lot of rows.所以我有一个 dataframe ,其中有几列和很多行。

Now I want to create a new column (C) which adds values of another column (A) as a string together if a third column (B) is identical.现在我想创建一个新列 (C),如果第三列 (B) 相同,它将另一列 (A) 的值作为字符串添加在一起。

So each 'group' (that is identical in B) should have a different string than the other groups in that column in the end.因此,每个“组”(在 B 中相同)最后应该具有与该列中的其他组不同的字符串。

A一个 B New Column C新立柱 C
First第一的 1 1 First_Third第一_第三
Second第二 22 22 Second_Fourth Second_Fourth
Third第三 1 1 First_Third第一_第三
Fourth第四 22 22 Second_Fourth Second_Fourth

Something like this pseudo code:像这样的伪代码:

for x in df[B]:
if (x "is identical to" x "of another row"):
df[C] = df[C].cat(df[A])

How do I code an algorithm that can do this?我如何编写可以做到这一点的算法?

Try this:尝试这个:

df['C'] = df.groupby('B')['A'].transform(lambda x: '_'.join(x))

You can use:您可以使用:

df['C'] = df.groupby('B')['A'].transform('_'.join)

Or, if you want to keep only unique values:或者,如果您只想保留唯一值:

df['C'] = df.groupby('B')['A'].transform(lambda x: '_'.join(x.unique()))

output: output:

        A   B              C
0   First   1    First_Third
1  Second  22  Second_Fourth
2   Third   1    First_Third
3  Fourth  22  Second_Fourth

暂无
暂无

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

相关问题 如果来自其他列的条件,如何向列添加新值 - How to add new values to columns, if condition from another columns Pandas df:用另一列中的特定值填充新列中的值(具有多列的条件) - Pandas df: fill values in new column with specific values from another column (condition with multiple columns) Pandas 如何根据所有行的值、应用于整个数据帧的特定列值向数据帧添加新列 - Pandas how add a new column to dataframe based on values from all rows, specific columns values applied to whole dataframe Pandas 数据框 - 将前一列中与特定条件匹配的所有值相加并将其添加到新列中 - Pandas Data Frame - Sum all the values in a previous column which match a specific condition and add it to a new column Pandas/Python:如何根据其他列的值创建新列并将额外条件应用于此新列 - Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column 根据条件在 df 的新列中添加值 - Add values in new column of df based on a condition 如果字符串“包含”substring,则添加带有条件的新列? - Add a new column with condition if a string 'contains' substring? 如何根据列值向 dataframe 添加新列? - How to add new columns to the dataframe based on the column values? 如果值相同,如何检查 3 列是否相同并添加一个具有该值的新列? - How to check if 3 columns are same and add a new column with the value if the values are same? 如何基于其他现有列的条件添加具有值的新列? - How to add a new column with values based on conditions of other existing columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM