摆脱熊猫中的多个名字

时间:2018-10-31 00:51:50

标签: python pandas

我正在尝试将多名称列转换为python中的单行名称列。现在,列名看起来像这样:

   [('passengers', 'January'),
   ('passengers', 'February'),
   ('passengers', 'March'),
   ('passengers', 'April'),
   ('passengers', 'December'),
   'Total']

但是我试图得到这个:

  ['January',
   'February',
   'March',
   'April',
   'December',
   'Total']

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

不是大熊猫:-):

l=[('passengers', 'January'),
   ('passengers', 'February'),
   ('passengers', 'March'),
   ('passengers', 'April'),
   ('passengers', 'December'),
   'Total'] # Or pandas then get data to list.
print([i[-1] if isinstance(i,tuple) else i for i in l])

输出:

['January', 'February', 'March', 'April', 'December', 'Total']

答案 1 :(得分:0)

自从提到熊猫

pd.Series(l).apply(pd.Series).ffill(1)[1]
Out[92]: 
0     January
1    February
2       March
3       April
4    December
5       Total
Name: 1, dtype: object

pd.Series(l).apply(pd.Series).ffill(1)[1].tolist()
Out[93]: ['January', 'February', 'March', 'April', 'December', 'Total']