简体   繁体   English

如何在python中将列表列表转换为Pandas数据框

[英]How to convert list of lists into a Pandas dataframe in python

I have a list as follows and need to covert it a pandas dataframe.我有一个列表如下,需要将它转换为熊猫数据框。

mylist = [[2000, 0.5, 0.3, 0.8, 0.9, 0.8], [2001, 0.5, 0.6, 0.8, 0.9, 0.9], [2002, 0.5, 0.3, 0.8, 0.8, 0.8], [2003, 0.9, 0.9, 0.9, 0.9, 0.8]]
columns = ['year', 'score_1', 'score_2', 'score_3', 'score_4', 'score_5']

I want the dataframe to be as follows.我希望数据框如下。

    year score_1 score_2 score_3 score_4 score_5
0    2000   0.5    0.3     0.8      0.9     0.8
1    2001   0.5    0.6     0.8      0.9     0.9
2    2002   0.5    0.3     0.8      0.8     0.8
3    2003   0.9    0.9     0.9      0.9     0.8

Currently, I am following the following code.目前,我正在关注以下代码。 But it needs to restructure my original 'mylist' data as 'year' and 'scores'.但它需要将我原来的“mylist”数据重组为“year”和“scores”。

pd.DataFrame(data=[scores],index=[year],columns=columns)

Therefore, I would like to know if there is any easy way of doing this in pandas.因此,我想知道在熊猫中是否有任何简单的方法可以做到这一点。

I am happy to provide more details if needed.如果需要,我很乐意提供更多详细信息。

If need only columns pass mylist :如果只需要列通过mylist

df = pd.DataFrame(mylist,columns=columns)
print (df)

   year  score_1  score_2  score_3  score_4  score_5
0  2000      0.5      0.3      0.8      0.9      0.8
1  2001      0.5      0.6      0.8      0.9      0.9
2  2002      0.5      0.3      0.8      0.8      0.8
3  2003      0.9      0.9      0.9      0.9      0.8

But if need index by years use dictionary comprehension with DataFrame.from_dict :但是如果需要按年索引,请使用DataFrame.from_dict字典理解:

df = pd.DataFrame.from_dict({x[0]: x[1:] for x in mylist},columns=columns[1:], orient='index')
print (df)

      score_1  score_2  score_3  score_4  score_5
2000      0.5      0.3      0.8      0.9      0.8
2001      0.5      0.6      0.8      0.9      0.9
2002      0.5      0.3      0.8      0.8      0.8
2003      0.9      0.9      0.9      0.9      0.8

And if need set index names add DataFrame.rename_axis :如果需要设置索引名称添加DataFrame.rename_axis

d = {x[0]: x[1:] for x in mylist}
df = pd.DataFrame.from_dict(d,columns=columns[1:], orient='index').rename_axis(columns[0])
print (df)

      score_1  score_2  score_3  score_4  score_5
year                                             
2000      0.5      0.3      0.8      0.9      0.8
2001      0.5      0.6      0.8      0.9      0.9
2002      0.5      0.3      0.8      0.8      0.8
2003      0.9      0.9      0.9      0.9      0.8

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

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