Can pandas.DataFrame have a list type column?

Is it possible to create a pandas.DataFrame that includes a list type field?

For example, I would like to load the following csv into pandas.DataFrame:

id,scores
1,"[1,2,3,4]"
2,"[1,2]"
3,"[0,2,4]"
+4
source share
2 answers

Separate double quotes:

id,scores
1, [1,2,3,4]
2, [1,2]
3, [0,2,4]

And you have to do this:

query = [[1, [1,2,3,4]], [2, [1,2]], [3, [0,2,4]]]
df = pandas.DataFrame(query, columns=['id', 'scores'])
print df
+3
source

You can use:

import pandas as pd
import io

temp=u'''id,scores  
1,"[1,2,3,4]"
2,"[1,2]"
3,"[0,2,4]"'''

df = pd.read_csv(io.StringIO(temp), sep=',', index_col=[0] )
print df
     scores  
id           
1   [1,2,3,4]
2       [1,2]
3     [0,2,4]

But dtype columns:, objectnot a list.

One approach uses astand converters:

import pandas as pd
import io
from ast import literal_eval

temp=u'''id,scores
1,"[1,2,3,4]"
2,"[1,2]"
3,"[0,2,4]"'''

def converter(x):
    #define format of datetime
    return literal_eval(x)

#define each column
converters={'scores': converter}

df = pd.read_csv(io.StringIO(temp), sep=',', converters=converters)
print df
   id        scores
0   1  [1, 2, 3, 4]
1   2        [1, 2]
2   3     [0, 2, 4]

#check lists:
print 2 in df.scores[2]
#True

print 1 in df.scores[2]
#False
+1
source

Source: https://habr.com/ru/post/1619331/


All Articles