Fresh Panda
Fresh Panda

Reputation: 23

Pandas series to row of dataframe using loc but some columns are missing

I want to add a series to a row of a dataframe but not every column is in the dataframe. How can I change the code:

df = pd.DataFrame(index=[0,1,2], columns = ['A', 'B'])
series = pd.Series(data=[5,3], index=['B', 'C'])

df.loc[0] = series
print(df)

Output:
     A    B
0  NaN    5
1  NaN  NaN
2  NaN  NaN

such that it automatically adds missing columns to the dataframe and I get this

Output:
     A    B    C
0  NaN    5    3
1  NaN  NaN  NaN
2  NaN  NaN  NaN

So far I tried this but got an keyError:

df.loc[0, series.index] = series

Output:
KeyError: "['C'] not in index"

Upvotes: 2

Views: 780

Answers (2)

Mykola Zotko
Mykola Zotko

Reputation: 17834

You can convert your series to DataFrame and use combine_first or combine:

df.combine_first(series.to_frame(name=0).T) # to fill the second row set 'name' to 1

Output:

     A    B    C
0  NaN  5.0  3.0
1  NaN  NaN  NaN
2  NaN  NaN  NaN

Upvotes: 0

mck
mck

Reputation: 42352

Not very elegant, but you can do it in a for loop:

for c in series.index:
    df.loc[0, c] = series[c]

df
     A    B    C
0  NaN    5  3.0
1  NaN  NaN  NaN
2  NaN  NaN  NaN

Another way is to add null columns to df before assigning to series:

df.reindex(columns=df.columns.tolist() + [c for c in series.index if c not in df.columns])
df.loc[0, series.index] = series

df
     A    B    C
0  NaN    5  3.0
1  NaN  NaN  NaN
2  NaN  NaN  NaN

Upvotes: 1

Related Questions