pandas.DataFrame 可以有列表类型列吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34197519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:22:04  来源:igfitidea点击:

Can pandas.DataFrame have list type column?

pythonpandas

提问by rkjt50r983

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

是否可以创建包含列表类型字段的 pandas.DataFrame?

For example, I'd like to load the following csv to pandas.DataFrame:

例如,我想将以下 csv 加载到 pandas.DataFrame:

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

采纳答案by Sameer Mirji

Strip the double quotes:

去掉双引号:

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

And you should be able 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

回答by jezrael

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 of column scores is object, not list.

但是列分数的 dtype 是object,而不是列表。

One approach use astand converters:

一种方法使用astconverters

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