Pandas How to Align Two Columns in a DataFrame and NaN empty cells

Viewed 147

I'm using Python 3.8.8

I have a DataFrame structured like this:

A B
0 1
1 2
2 1
3 7
4 7
5 8

and an array:

C = [3, 4, 7]

I would like to add an array "C" as a new column to the DataFrame. The problem is this array has a different length of index than the df. I would like to make up for the difference in length in C by filling the empty cells with NaNs. My desired result would look something like:

A B C
0 1 NaN
1 2 NaN
2 1 3
3 7 4
4 7 7
5 8 NaN

What I am looking for specifically is a way to add C starting at a specific index of the df, but I don't know how to work around the discrepancy between the length of the df and array.

Thank you for your time

1 Answers

To get around the problem of 'different length' when putting your list into the dataframe, you can convert it to a pandas series. Once you do that, you can easily add it to your dataframe with the rest of the values being filled with np.nan.

In your case, you can specifically also set the index when you convert your C list to a series, which you can then assign to your dataframe. Pandas nature to align data on indices will place the series on the right index

Consider using the code below:

c = pd.Series([3, 4, 7],index=[2,3,4])
df['C'] = c

prints:

   A  B    0
0  0  1  NaN
1  1  2  NaN
2  2  1  3.0
3  3  7  4.0
4  4  7  7.0
5  5  8  NaN

Renaming 0 should be trivial.

Related