Tuesday 15 January 2013

pandas - DataFrame from dictionary -



pandas - DataFrame from dictionary -

sorry, if duplicate, didn't find solution in internet...

i have dictionary

{'a':1, 'b':2, 'c':3}

now want build pandas df columns names corresponding key , values corresponding values. should df 1 row.

a b c 1 2 3

at other topic found solutions, both - keys , values columns in new df.

you have caveats here, if pass dict dataframe constructor raise error:

valueerror: if using scalar values, must must pass index

to around can pass index work:

in [139]: temp = {'a':1,'b':2,'c':3} pd.dataframe(temp, index=[0]) out[139]: b c 0 1 2 3

ideally values should iterable, list or array like:

in [141]: temp = {'a':[1],'b':[2],'c':[3]} pd.dataframe(temp) out[141]: b c 0 1 2 3

thanks @joris pointing out if wrap dict in list don't have pass index constructor:

in [142]: temp = {'a':1,'b':2,'c':3} pd.dataframe([temp]) out[142]: b c 0 1 2 3

dictionary pandas

No comments:

Post a Comment