简体   繁体   中英

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.

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.

So each 'group' (that is identical in B) should have a different string than the other groups in that column in the end.

A B New Column C
First 1 First_Third
Second 22 Second_Fourth
Third 1 First_Third
Fourth 22 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM