我有一个同时包含list
和str
值的pandas数据框列。我只是试图将str
的值转换成正确的list
格式,以便它与其他list
形式的值匹配。我找到了解决方法,但是我想看看是否有更好的方法?以下是我的问题:
regex
,replace
,.. etc?[nan]
转换为[]
?这是我的尝试:
StudentName,CourseID
Alan,"['abc-12-0878', 'abc-12-45', 'abc-12-232342']"
Tim,"['abc-12-0878', 'abc-12-45']"
David,abc-12-1147
Martha,
Matt,"['abc-12-0878', 'abc-12-45']"
Abby,abc-12-1148
import pandas as pd
df = pd.read_csv('sample_students.csv')
df
df['result'] = df['CourseID'].astype(str).apply(lambda x: x.strip('[]').replace("'","").split(','))
# Regex route.
# Pandas`s build in function available?
# gives `[nan]` instead of `[]`
# `to_list` and `tolist` didn't work.
print(df[['CourseID','result']])
CourseID result
['abc-12-0878', 'abc-12-45', 'abc-12-232342'] ['abc-12-0878', 'abc-12-45', 'abc-12-232342']
['abc-12-0878', 'abc-12-45'] ['abc-12-0878', 'abc-12-45']
abc-12-1147 ['abc-12-1147']
NaN []
['abc-12-0878', 'abc-12-45'] ['abc-12-0878', 'abc-12-45']
abc-12-1148 [abc-12-1148]
答案 0 :(得分:0)
您可以应用ast.literal_eval()来解析列表的文字表示形式。
import ast
def f(s):
if pd.isna(s): # case 1: nan
return []
elif s[0] == "[": # case 2: string of list
return ast.literal_eval(s)
else: # case 3: string
return [s]
df["result"] = df["CourseID"].apply(f)
或单线:
df["result"] = df["CourseID"].apply(lambda s: [] if pd.isna(s) else ast.literal_eval(s) if s[0] == "[" else [s])
结果:
print(df[["CourseID","result"]])
CourseID result
0 ['abc-12-0878', 'abc-12-45', 'abc-12-232342'] [abc-12-0878, abc-12-45, abc-12-232342]
1 ['abc-12-0878', 'abc-12-45'] [abc-12-0878, abc-12-45]
2 abc-12-1147 [abc-12-1147]
3 NaN []
4 ['abc-12-0878', 'abc-12-45'] [abc-12-0878, abc-12-45]
5 abc-12-1148 [abc-12-1148]
答案 1 :(得分:0)
如果您不想导入其他库,则可以这样进行:
def update_data(val):
if pd.isna(val):
return []
if val[0] == '[':
return val
return [val]
df['Result'] = df.apply(lambda row: update_data(row['CourseID']), axis= 1)
答案 2 :(得分:0)
您只能检查值的类型
df['result'] = df['CourseID'].apply(lambda x: x.strip('[]').replace("'","").split(',') if type(x) == str else [])
>>> print(df[['CourseID','result']])
CourseID result
0 ['abc-12-0878', 'abc-12-45', 'abc-12-232342'] [abc-12-0878, abc-12-45, abc-12-232342]
1 ['abc-12-0878', 'abc-12-45'] [abc-12-0878, abc-12-45]
2 abc-12-1147 [abc-12-1147]
3 NaN []
4 ['abc-12-0878', 'abc-12-45'] [abc-12-0878, abc-12-45]
5 abc-12-1148 [abc-12-1148]