简体   繁体   English

如何将列表列表转换为数据帧并将列表的第一个元素作为索引

[英]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? 有可能我可以获得预期的pandas数据帧结构吗? Thanks for all your kindly checking in advance. 感谢您提前检查所有。

You can call set_index on the result of the dataframe: 您可以在set_index的结果上调用set_index

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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