Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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'.

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.

like image 259
EmJ Avatar asked Oct 31 '25 15:10

EmJ


1 Answers

If need only columns pass 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:

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:

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
like image 121
jezrael Avatar answered Nov 03 '25 06:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!