what i'm asking must be something very easy, but i honestly can't see it.... :(
I have an array, lets say
data = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10,11,12]]
and i want to put it in a dataframe.
I do df = pd.Dataframe(data, columns={'col1', 'col2', 'col3'})
aiming for:
col1 col2 col3
1 2 3
4 5 6
7 8 9
10 11 12
but i am getting:
col3 col1 col2
1 2 3
4 5 6
7 8 9
10 11 12
(notice the discrepancy between column names and data)
I know i can re-arrange the column names order in the dataframe creation, but i'm trying to understand how it works.
Am i doing something wrong, or it's normal behaviour? (why though?)
You have to pass a tuple
or list
as value for columns
property.
In your example you're using a set
of columns which is an unordered collection.
df = pd.DataFrame(data, columns=['col1', 'col2', 'col3'])
You are are using a {set}
of columns, which is NOT an ordered collection (neither are dictionaries). Try with a (tuple)
, o simply a [list]
df = pd.Dataframe(data, columns=['col1', 'col2', 'col3'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.