简体   繁体   中英

How to covert a list of lists into dataframe and make the first element of the lists as the index

The sample data of mine may be seen here:

data=[['Australia',100],['France',200],['Germany',300],['America',400]]

What I expect may be the dataframe like this:

           volume
Australia     100
France        200
Germany       300
America       400

And I've tried the following:

pd.DataFrame(data,columns=['Country','Volume']) 
     Country  Volume
0  Australia     100
1     France     200
2    Germany     300
3    America     400

pd.DataFrame.from_items()

Howerver, I still can't get the expected result?

Is there a possible way that I can get the expected pandas dataframe structure? Thanks for all your kindly checking in advance.

You can call set_index on the result of the dataframe:

In [2]:
data=[['Australia',100],['France',200],['Germany',300],['America',400]]
pd.DataFrame(data,columns=['Country','Volume']).set_index('Country') 

Out[2]:
           Volume
Country          
Australia     100
France        200
Germany       300
America       400

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM