Python 将列表的字符串表示形式转换为dataframe列中的列表
我有这样的cvs文件:Python 将列表的字符串表示形式转换为dataframe列中的列表,python,python-3.x,csv,dataframe,Python,Python 3.x,Csv,Dataframe,我有这样的cvs文件: number file 1 [file1,file2] 2 [file1] 3 [file3,file4] >>> df = pd.read_csv('file.csv') >>> for col in df: >>> print (df[col].apply(type)) <class 'int'> <class 'int'> &l
number file
1 [file1,file2]
2 [file1]
3 [file3,file4]
>>> df = pd.read_csv('file.csv')
>>> for col in df:
>>> print (df[col].apply(type))
<class 'int'>
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>
....
列的数据类型为文件
为列表
。但当我再次读取该文件时,我得到了列file
的数据类型是str
。输出如下:
number file
1 [file1,file2]
2 [file1]
3 [file3,file4]
>>> df = pd.read_csv('file.csv')
>>> for col in df:
>>> print (df[col].apply(type))
<class 'int'>
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>
....
df=pd.read\u csv('file.csv')
>>>对于df中的col:
>>>打印(df[col]。应用(类型))
....
但是,我想将该值作为列表来读取。我已经试过了,但我无法将其应用到数据帧。有人能帮我解决这个问题吗
谢谢如果您确实希望每个数字有多个文件,则应将输入转换为:
number file
1 file1
1 file2
2 file1
3 file3
3 file4
然后,要将其作为列表选择,您可以执行以下操作:
df[df["number"] == 1]
或者你想要的任何数字。你尝试时出现了什么错误?@MedAli我更新了问题,我想[应用到我的数据帧。但是,我不知道如何应用