熊猫:将某些str列转换为其他值列表

时间:2020-10-30 20:37:21

标签: python-3.x pandas dataframe python-3.6

我有一个同时包含liststr值的pandas数据框列。我只是试图将str的值转换成正确的list格式,以便它与其他list形式的值匹配。我找到了解决方法,但是我想看看是否有更好的方法?以下是我的问题:

  1. 如果要使用熊猫的构建功能/能力来代替编写长的regexreplace,.. etc?
  2. 如何不使用正则表达式将[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]

3 个答案:

答案 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]