Code Sample, a copy-pastable example if possible
Using pd.series works fine:
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 this:
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
Problem description
Considering that a dataframe can be created using a dictionary, it seems odd that adding to a dataframe using a dictionary would result in shuffled columns when they have been explicitly labeled.
This seems like an unexpected behaviour which, although may not be a bug, doesn't seem like the optimal way of making this process function.
Expected Output
Adding a row to a dataframe using a dictionary which gives column headers results in the same thing as adding a row using pd.series
Output of pd.show_versions()
Comment From: gfyoung
@mat2py : Thanks for the issue! I tried your example using 0.20.2
on Windows, and I can't seem to reproduce your error (I just copied and pasted your code). If you could reconfirm that you get this error by copying and pasting the code you provided above?
Also, if anyone else can reproduce this (cc @jreback ), that would be great to know.
Comment From: mp-v2
I can replicate it yes. However the issue doesn't occur if you apply the dict to the df after already having applied the pd.series. You have to recreate the dataframe and only try to add the dict.
What you probably did: - create df - test with df.series (works) - test with dict (works)
What doesn't work: - create df - test with dict (doesn't work)
Comment From: mp-v2
Comment From: gfyoung
Not on my computer ATM, but can't argue with pictures. Something funny is going on there. PR to patch this is welcome!
Comment From: chris-b1
Thanks for the example, this is a duplicate of #16724