Difference between giving pandas a python iterable vs a pd.Series for column

Viewed 131

What are some of the differences between passing a List vs a pd.Series type to create a new dataFrame column? For example, from trial-and-error I've noticed:

# (1d) We can also give it a Series, which is quite similar to giving it a List
df['cost1'] = pd.Series([random.choice([1.99,2.99,3.99]) for i in range(len(df))])
df['cost2'] =           [random.choice([1.99,2.99,3.99]) for i in range(len(df))]
df['cost3'] = pd.Series([1,2,3]) # <== will pad length with `NaN`
df['cost4'] =           [1,2,3]  # <== this one will fail because not the same size
d

Are there any other reasons that pd.Series differs from passing a standard python list? Can a dataframe take any python iterable or are there restrictions on what can be passed to it? Finally, is using pd.Series the 'correct' way to add columns, or can it be used interchangably with other types?

1 Answers

List assign to dataframe here require the same length

For the pd.Series assign , it will use the index as key to match original DataFrame index, then fill the value with the same index in Series

df=pd.DataFrame([1,2,3],index=[9,8,7])
df['New']=pd.Series([1,2,3])
 # the default index is range index , which is from 0 to n 
 # since the dataframe index dose not match the series, then will return NaN 
df
Out[88]: 
   0  New
9  1  NaN
8  2  NaN
7  3  NaN

Different length with matched index

df['New']=pd.Series([1,2],index=[9,8])
df
Out[90]: 
   0  New
9  1  1.0
8  2  2.0
7  3  NaN
Related