python - why DataFrame in pandas arrange number before string? -
data1 = {'key':['b','b','a','c','a','a','b'], 'data':range(7)} df1 = pd.dataframe(data1)
it arranged data, follow key. why not arranged in key, data?
do need specify columns?
data1
dictionary elements stored in arbitrary order. now, reason see in specific order every time because pandas frame constructor sort dictionary keys if there no explicit columns argument.
constructor uses:
>>> sorted(['key', 'data']) ['data', 'key']
so going same ordering of columns every time. can (have to) give columns argument explicitly store them in order want.
>>> pd.dataframe(data1, columns=['key', 'data']) key data 0 b 0 1 b 1 2 2 3 c 3 4 4 5 5 6 b 6
Comments
Post a Comment