I found a behavior in pandas DataFrames that I do not understand.
df = pd.DataFrame(np.random.randint(1, 10, (3, 3)), index=['one', 'one', 'two'], columns=['col1', 'col2', 'col3'])
new_data = pd.Series({'col1': 'new', 'col2': 'new', 'col3': 'new'})
df.iloc[0] = new_data
But if I try to add a dictionary instead, I get the following:
new_data = {'col1': 'new', 'col2': 'new', 'col3': 'new'}
df.iloc[0] = new_data
Why is this happening? In the process of writing this question, I realized that most likely df.loc uses only the keys from new_data, which also explains why the values are out of order. But then again, why is this so? If I try to create a DataFrame from a dictionary, it processes the keys as if they were columns:
pd.DataFrame([new_data])
# col1 col2 col3
#0 new new new
Why is this not the default behavior in df.loc?
Jones source
share