合并连续列中相同变量的级别

时间:2019-05-28 04:52:05

标签: python pandas csv data-transform

我有一个csv数据文件,其中包含2个标头,这意味着一个标头是一个问题,第二个标头是一个子标头,它对主标头具有多个级别或答案。当前的csv如下表所示

Header  Which country do you live?        Which country you previously visited? 
Users    Canada  USA   UK  Mexico  Norway India  Singapore  Pakistan 
User 1   Canada                                  Singapore
User 2                 UK                 India              
User 3                     Mexico                           Pakistan
User 4                             Norway India 

我需要将其转换为下表

Users   Which country do you live?  Which country you previously visited?
User 1  Canada                      Singapore
User 2  UK                          India
User 3  Norway                      Pakistan
User 4  Mexico                      India

有人可以帮我吗?

这是我的数据的样子 enter image description here

我的输入文件如下所示 enter image description here 这就是我最终输出的样子 enter image description here

1 个答案:

答案 0 :(得分:2)

首先用bfill回填缺失值,然后选择第一列并通过DataFrame.droplevel删除第二级MultiIndex

print (df.columns)
MultiIndex(levels=[['Header', 'Which country do you live?'],
                   ['Canada', 'Mexico', 'UK', 'USA', 'Users']],
           codes=[[0, 1, 1, 1, 1], [4, 0, 3, 2, 1]])

#if first column is not index, create it
#df = df.set_index([df.columns[0]])
#if empty strings repalce them to NaNs
#df = df.replace('', np.nan)

df = df.bfill(axis=1).iloc[:, 0].reset_index().droplevel(level=1, axis=1)
print (df)
   Header Which country do you live?
0  User 1                     Canada
1  User 2                         UK
2  User 3                     Mexico
3  User 4                     Norway

编辑:

df = df.groupby(level=0, axis=1).apply(lambda x: x.bfill(axis=1).iloc[:, 0])
print (df)
   Header Which country do you live? Which country you previously visited?
0  User 1                     Canada                             Singapore
1  User 2                         UK                                 India
2  User 3                     Mexico                              Pakistan
3  User 4                     Norway                                 India