Pandas correcting for nested list of list in column headers

jarthoben :

I have scraped some html data and read into pandas with beautiful soup.

Unfortunately, due to the structure of the html, the pandas column headers are a nested list of list, which I don't want.

Here is what I have:

headers = list(df.columns.values)
print(headers)
[('Year:', 'Length:'), ('2019', '12 Months'), ('2018', '12 Months'), ('2017', '12 Months'), ('2016', '12 Months')]

I only want the first list to be the column headers:

['Year:', '2019', '2018', '2017', '2016']

In the following loop code I can isolate the first list that I want:

for sublist in headers:
    print(sublist[0])
Year:
2019
2018
2017
2016

But how do I assign the output of this loop to become my new pandas column headers?

Many thanks!

YOBEN_S :

We have droplevel

df=df.droplevel(axis=1,level=1)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405697&siteId=1