Update pandas DataFrame string with dictionary

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
# resulting df looks like:

#       col1    col2    col3
#one    new     new     new
#one    9       6       1
#two    8       3       7

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
#
#         col1  col2    col3
#one      col2  col3    col1
#one      2     1       7
#two      5     8       6

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?

+4
source share
1 answer

This is the difference between how the iteration of the dictionary and how the pandas series is handled.

pandas , . , .

pandas , , pandas . , . .

+4

All Articles