pandas Gotchas of pandas Automatic Data Alignment (index-awared behaviour)


Example

If you want to append a series of values [1,2] to the column of dataframe df, you will get NaNs:

import pandas as pd

series=pd.Series([1,2])
df=pd.DataFrame(index=[3,4])
df['col']=series
df

   col
3    NaN
4    NaN

because setting a new column automatically aligns the data by the indexe, and your values 1 and 2 would get the indexes 0 and 1, and not 3 and 4 as in your data frame:

df=pd.DataFrame(index=[1,2])
df['col']=series
df

   col
1      2.0
2      NaN

If you want to ignore index, you should set the .values at the end:

df['col']=series.values

   col
3    1
4    2