How to create a new column of NaN when adding a series to a DataFrame?

Viewed 63

This question might be confusing but I'm trying to add a series as a row to an already created DataFrame. Though if there's a column that's not already been created in the DataFrame I want to add one will NaN and the value from the series.

Here's my series:

w1    0.195870
w3    0.072609
w4    0.332842
w5    0.567370

dtype: float64

and here's my dataframe:

enter image description here

When I do "df.loc[3] = series" I get

enter image description here

I want there to be a completely new column called "w5" filled with NaN all above index 3.

1 Answers

How about reindex first with a union of df.columns and series.index:

df = df.reindex(columns = (df.columns | series.index))

df.loc[3] = series

Toy example

import pandas as pd
import numpy as np

# Setup
series = pd.Series({
 'w1': 0.19587,
 'w3': 0.072609,
 'w4': 0.332842,
 'w5': 0.56737})
series

np.random.seed(0)
df = pd.DataFrame(np.random.randn(3, 4), columns=['w1' ,'w2', 'w3' ,'w4'])

print(df)

[out]

         w1        w2        w3        w4
0  1.764052  0.400157  0.978738  2.240893
1  1.867558 -0.977278  0.950088 -0.151357
2 -0.103219  0.410599  0.144044  1.454274

# Reindex
df = df.reindex(columns = (df.columns | series.index))

df.loc[3] = series

[out]

         w1        w2        w3        w4       w5
0  1.764052  0.400157  0.978738  2.240893      NaN
1  1.867558 -0.977278  0.950088 -0.151357      NaN
2 -0.103219  0.410599  0.144044  1.454274      NaN
3  0.195870       NaN  0.072609  0.332842  0.56737
Related