您可以使用str.split 以逗号分隔字符串,使用expand=True 这会将每个值分隔到它自己的列中,然后您可以将类型转换为int,然后根据需要获取列表中的值:
In [109]:
df['Values'].str.split(',',expand=True).astype(int).values.tolist()
Out[109]:
[[1, 2, 3, 4], [2, 1, 0, 6], [9, 8, 7, 6]]
分解:
In [110]:
df['Values'].str.split(',',expand=True)
Out[110]:
0 1 2 3
0 1 2 3 4
1 2 1 0 6
2 9 8 7 6
In [111]:
df['Values'].str.split(',',expand=True).astype(int).info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
0 3 non-null int32
1 3 non-null int32
2 3 non-null int32
3 3 non-null int32
dtypes: int32(4)
memory usage: 128.0 bytes
要处理NaN/None 值,请使用to_numeric 和stack 和unstack:
In [114]:
pd.to_numeric(df['Values'].str.split(',',expand=True).stack(), errors='coerce').unstack().values.tolist()
Out[114]:
[[1, 2, 3, 4], [2, 1, 0, 6], [9, 8, 7, 6]]