简体   繁体   English

使用循环的数据框的Concat列-Python

[英]Concat columns of a Dataframe using a loop - Python

I want to concatenate the values of several columns of a dataframe using a loop. 我想使用循环来连接数据帧的几列的值。

You can find the actual dataframe : 您可以找到实际的数据框:

 Artist_1                Artist_2   Artist_3
Lady Antebellum              ?         ?
Reba McEntire                ?         ?
Wanda Jackson                ?         ?
Carrie Underwood             ?         ?
       ?                     ?         ?
The Bellamy Brothers         ?         ?
Keith Urban          Miranda Lambert   ?
Sam Hunt                     ?         ?
Johnny Cash                  ?         ?
Johnny Cash            June Carter     ?
Highwaymen                   ?         ?
Loretta Lynn                 ?         ?
Sissy Spacek                 ?         ?
Loretta Lynn         Sheryl Crow    Miranda Lambert
Charley Pride                ?         ?

and the expected result : 和预期的结果:

Artist
Lady Antebellum
Reba McEntire
Wanda Jackson
Carrie Underwood
?
The Bellamy Brothers
Keith Urban, Miranda Lambert
Sam Hunt
Johnny Cash
Johnny Cash, June Carter
Highwaymen
Loretta Lynn
Sissy Spacek
Loretta Lynn,  Sheryl Crow, Miranda Lambert
Charley Pride

This is one way using pd.DataFrame.apply / str.join followed by pd.Series.replace to account for instances where no names exist: 这是使用pd.DataFrame.apply / str.joinpd.Series.replace一种方式来说明不存在名称的实例:

import pandas as pd

df = pd.DataFrame({'Artist_1': ['A', 'B', '?', 'D', '?', 'E'],
                   'Artist_2': ['?', '?', '?', 'G', '?', 'I'],
                   'Artist_3': ['J', '?', '?', '?', 'M', 'N']})

df['Artist_All'] = df.apply(lambda x: ', '.join([i for i in x if i != '?']), axis=1)\
                     .replace('', '?')

print(df)

  Artist_1 Artist_2 Artist_3 Artist_All
0        A        ?        J       A, J
1        B        ?        ?          B
2        ?        ?        ?          ?
3        D        G        ?       D, G
4        ?        ?        M          M
5        E        I        N    E, I, N

Alternatively, you can use a list comprehension: 或者,您可以使用列表推导:

df['Artist_All'] = [', '.join([i for i in x if i != '?']) for x in df.values]
df['Artist_All'] = df['Artist_All'].replace('', '?')

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

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