How to do not loose rows with NaN when stack/unstak?

Viewed 41

I have a set of data running from 1945 to 2020, for a series of material produced in two countries. To create a dataframe I concat different df.

df = pd.concat([ProdCountry1['Producta'], [ProdCountry2['Producta'], [ProdCountry1['Productb'],  [ProdCountry2['Productb'], ...] ...)

With axis=1, the keys and names, etc.

I get this kind of table:

enter image description here

Then I stack this dataframe to get out the NaNs in rows index (years), but then I loose the years 1946/1948/1949, which are with NaNs only.

df = df.stack()

Here is the kind of df I get when I unstack it:

enter image description here

So, my question is: how can I avoid loosing the years with NaN rows in my df? I need them to interpolate and work later in my notebook.

Thanks in advance for your help.

2 Answers

Let us try dropna

df = df.dropna(how='all')

There is a dropna parameter to the stack method pass it as false

 DataFrame.stack(level=- 1, dropna=True)

Cf Documentation for pandas.DataFrame.stack

Related