jarthoben
jarthoben

Reputation: 129

Pandas correcting for nested list of list in column headers

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!

Upvotes: 0

Views: 20

Answers (1)

BENY
BENY

Reputation: 323376

We have droplevel

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

Upvotes: 2

Related Questions