Newbielp
Newbielp

Reputation: 532

Dataframe saved as csv disappears the columns with nan values (python). Any solution?

I have a df where some columns host totally nan values and when I save the df as csv, these columns do not exist.

I have tried some solutions I found online, such as convert every column astype('object') or astype('int') or to .replace(np.nan, '', regex=True)''' or to .replace(np.nan, 'empty', regex=True)''' et cetera but none worked.

To give you some background info, the df accrued in the following way:

 In[]: dataset
Out[]:
       B3A    T3A     C2
 1    67.4    NaN    1.9
 2    25.5    NaN    NaN
 3     NaN    1.7   11.4
 4     NaN    4.0    NaN
 5     NaN   11.5   27.1

 In[]: df = dataset.reindex(columns=['B2', 'C1', 'T3A', 'C2', 'B3A'])
 In[]: df
Out[]:
   B2  C1   T3A    C2   B3A
1 NaN NaN   NaN   1.9  67.4
2 NaN NaN   NaN   NaN  25.5
3 NaN NaN   1.7  11.4   NaN
4 NaN NaN   4.0   NaN   NaN
5 NaN NaN  11.5  27.1   NaN

So when I do something like df.to_csv('df.csv') the column B2 and C1 just do not appear.

Is there a way to have all the nan columns in the csv? I would not be able to insert them manually as the previous is a small example and of course there is a point for the columns to follow the specific order.

Thank you in advance!

Upvotes: 1

Views: 466

Answers (1)

jezrael
jezrael

Reputation: 862541

You can use CategoricalIndex for DataFrame.reindex, but in some oldier/ another pandas version also working your solution:

new = pd.CategoricalIndex(['B2', 'C1', 'T3A', 'C2', 'B3A'], ordered=True)
df = dataset.reindex(columns=new)

Upvotes: 2

Related Questions