Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表列表转换为dataframe并将列表的第一个元素作为索引_Python_Pandas - Fatal编程技术网

Python 如何将列表列表转换为dataframe并将列表的第一个元素作为索引

Python 如何将列表列表转换为dataframe并将列表的第一个元素作为索引,python,pandas,Python,Pandas,我的样本数据可以在这里看到: data=[['Australia',100],['France',200],['Germany',300],['America',400]] 我所期望的可能是这样的数据帧: volume Australia 100 France 200 Germany 300 America 400 我尝试了以下方法: pd.DataFrame(data,columns=['Country','Volume'

我的样本数据可以在这里看到:

data=[['Australia',100],['France',200],['Germany',300],['America',400]]
我所期望的可能是这样的数据帧:

           volume
Australia     100
France        200
Germany       300
America       400
我尝试了以下方法:

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

pd.DataFrame.from_items()
但是,我还是不能得到预期的结果

有没有一种可能的方法可以获得预期的数据帧结构?
感谢您提前进行的友好检查。

您可以调用
set\u 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

您只需执行pd.DataFrame(数据,列=['Country','Volume'])。设置索引('Country')Wow,太棒了!它起作用了。非常感谢,它真的帮助了我。