Convert a columns of string to list in pandas

前端 未结 4 1143
不思量自难忘°
不思量自难忘° 2020-12-03 15:27

I have a problem with the type of one of my column in a pandas dataframe. Basically the column is saved in a csv file as a string, and I wanna use it as a tuple to be able t

相关标签:
4条回答
  • 2020-12-03 15:57

    You can use ast.literal_eval, which will give you a tuple:

    import ast
    df.LABELS = df.LABELS.apply(ast.literal_eval)
    

    If you do want a list, use:

    df.LABELS.apply(lambda s: list(ast.literal_eval(s)))
    
    0 讨论(0)
  • 2020-12-03 16:09

    Alternatively, you might consider regular expressions:

    pattern = re.compile("[0-9]\.[0-9]")
    df.LABELS.apply(pattern.findall)
    
    0 讨论(0)
  • 2020-12-03 16:11

    You can try this (assuming your csv is called filename.csv):

    df = pd.read_csv('filename.csv')
    
    df['LABELS'] = df.LABELS.apply(lambda x: x.strip('()').split(','))
    
    >>> df
       ID                               LABELS
    0   1  [1.0, 2.0, 2.0, 3.0, 3.0, 1.0, 4.0]
    1   2  [1.0, 2.0, 2.0, 3.0, 3.0, 1.0, 4.0]
    
    0 讨论(0)
  • 2020-12-03 16:13

    Use str.strip and str.split:

    df['LABELS'] = df['LABELS'].str.strip('()').str.split(',')
    

    But if no NaNs here, list comprehension working nice too:

    df['LABELS'] = [x.strip('()').split(',') for x in df['LABELS']]
    
    0 讨论(0)
提交回复
热议问题