Reputation:
I am trying to convert multi-name column to a sigle-name column in python. Right now, the column names look like this:
[('passengers', 'January'),
('passengers', 'February'),
('passengers', 'March'),
('passengers', 'April'),
('passengers', 'December'),
'Total']
But I am trying to get this:
['January',
'February',
'March',
'April',
'December',
'Total']
Any suggestions?
Upvotes: 2
Views: 52
Reputation: 71610
Not pandas :-):
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])
Output:
['January', 'February', 'March', 'April', 'December', 'Total']
Upvotes: 1
Reputation: 323356
Since you mention pandas
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']
Upvotes: 0