Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
df1 = pd.DataFrame.from_dict({'a': ['A1', 'A2'], 'b': 'B'}, orient='index')
df2 = pd.DataFrame.from_dict({'b': 'B', 'a': ['A1', 'A2']}, orient='index')
Issue Description
df1 has columns [0, 1] with the array exploded df2 has only one column(0) with the array as value
Expected Behavior
Expected the order of the items of the dictionary to not impact on the result of the data frame. Since it's possible to pass a combination of scalars and lists, I would expect it to work the same regardless the dict items order.
Installed Versions
Comment From: dicristina
Make the values of your dictionaries array-like:
df1 = pd.DataFrame.from_dict({'a': ['A1', 'A2'], 'b': ['B']}, orient='index')
df2 = pd.DataFrame.from_dict({'b': ['B'], 'a': ['A1', 'A2']}, orient='index')
The documentation states that pd.DataFrame.from_dict
is to be used to "Construct DataFrame from dict of array-like or dicts". A string is not considered array-like in this case.
Comment From: topper-123
I agree those two operation should give the same rows, so this is a bug.
More generally, we normally treat listlikes in DataFrame/series constructors and I think we should to that here too, though that may be an enhancement, so goes beyond this concrete issue. Also I'd expect a scalar to explode rowwise , so I'd actually expect df1 to be:
0 1
a A1 A2
b B B
But that's also en enhancement,maybe.