pandas 如何将列表列表转换为数据框并将列表的第一个元素作为索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37068619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to covert a list of lists into dataframe and make the first element of the lists as the index
提问by Tian Wolf
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.
有没有可能的方法可以获得预期的Pandas数据框结构?感谢您提前检查。
回答by EdChum
You can call set_index
on the result of the dataframe:
您可以调用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