如何将多行pandas数据帧组合成两个其他行值python3之间的一个?

时间:2017-09-26 13:48:59

标签: python-3.x pandas

我有一个pandas数据框,其中包含一个列,其中包含以空格或na行分隔的姓名,地址和电话信息,如下所示:

   data
0  Business name one
1  1234 address ln
2  Town, ST 55655
3  (555) 555-5555
4  nan
5  Business name two
6  5678 address dr
7  New Town, ST 55677
8  nan
9  Business name three
10 nan

依旧......

我想要的是:

   Name                 Addr1             Addr2               Phone
0  Business name one    1234 address ln   Town, ST 55655      (555) 555-5555
1  Business name two    5678 address dr   New Town, ST 55677
2  Business name three  

我正在使用python 3并且卡住了,非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

您可以使用:

a = df['data'].isnull().cumsum().reindex(df.dropna().index)
print (a)
0    0
1    0
2    0
3    0
5    1
6    1
7    1
9    2
Name: data, dtype: int32
df = df.dropna().set_index([a, a.groupby(a).cumcount()])['data'].unstack()
df.columns = ['Name','Addr1','Addr2','Phone']
print (df)
                     Name            Addr1               Addr2           Phone
data                                                                          
0       Business name one  1234 address ln      Town, ST 55655  (555) 555-5555
1       Business name two  5678 address dr  New Town, ST 55677            None
2     Business name three             None                None            None

如果有多个address可以动态创建列:

df.columns = ['Name'] + 
             ['Addr{}'.format(x+1) for x in range(len(df.columns) - 2)] +
             ['Phone']

答案 1 :(得分:0)

df['group']=df['data'].str.contains('Business').cumsum().replace({True:1}).ffill()
df1=df.groupby('group')['data'].apply(list).apply(pd.Series).dropna(axis=1,thresh =1)
df1.columns=['Name','Addr1','Addr2','Phone']

df1
Out[1221]: 
                      Name            Addr1               Addr2  \
group                                                             
1.0      Business name one  1234 address ln      Town, ST 55655   
2.0      Business name two  5678 address dr  New Town, ST 55677   
3.0    Business name three              NaN                 NaN   
                Phone  
group                  
1.0    (555) 555-5555  
2.0               NaN  
3.0               NaN