Python 循环使用列名来创建列表
来了一个简单的。。我想从数据框中的每一列创建列表,并尝试在其上循环Python 循环使用列名来创建列表,python,pandas,list,loops,Python,Pandas,List,Loops,来了一个简单的。。我想从数据框中的每一列创建列表,并尝试在其上循环 for columnName in grouped.iteritems(): columnName = grouped[columnName] 它给了我一个类型错误:'('africa',year(注意,非洲是列之一,年份是索引)。有人知道这里发生了什么吗 这是我的数据帧 continent africa antarctica asia ... north america oceania sou
for columnName in grouped.iteritems():
columnName = grouped[columnName]
它给了我一个类型错误:'('africa',year
(注意,非洲是列之一,年份是索引)。有人知道这里发生了什么吗
这是我的数据帧
continent africa antarctica asia ... north america oceania south america
year ...
2009 NaN NaN 1.0 ... NaN NaN NaN
2010 94.0 1.0 306.0 ... 72.0 12.0 21.0
2011 26.0 NaN 171.0 ... 21.0 2.0 4.0
2012 975.0 28.0 5318.0 ... 480.0 58.0 140.0
2013 1627.0 30.0 7363.0 ... 725.0 124.0 335.0
2014 3476.0 41.0 7857.0 ... 1031.0 202.0 520.0
2015 2999.0 43.0 12048.0 ... 1374.0 256.0 668.0
2016 2546.0 55.0 11429.0 ... 1798.0 325.0 3021.0
2017 7486.0 155.0 18467.0 ... 2696.0 640.0 2274.0
2018 10903.0 340.0 22979.0 ... 2921.0 723.0 1702.0
2019 7367.0 194.0 15928.0 ... 1971.0 457.0 993.0
[11 rows x 7 columns]
因此,我希望得到一个列表,每个列包含11个元素。iteritems返回一对列名称,列数据,类似于python的dict.items()。如果要迭代列名,可以像这样迭代分组:
result = {}
for column_name in grouped:
result[column_name] = [*grouped[column_name]]
这将给您留下一个包含
result
中的纯python列表的纯python dict。请注意,如果只执行result[column\u name]=grouped[column\u name],您将得到pandas系列而不是列表
您能发布预期结果吗?完成。如果不清楚,请告诉我clearDictionary是否也能正常工作,同时感谢您提供有关series vs.list的提示。