Padfoot123
Padfoot123

Reputation: 1117

Convert a Pandas dataframe cell to list

I have a dataframe which has the below columns

Record Type     Value
100             1,2,3,4,5
200             0,10
300             1

Expected results: list1 = [1,2,3,4,5]

Upvotes: 5

Views: 13231

Answers (3)

Marek
Marek

Reputation: 61

Maybe this way:

import pandas as pd
df = pd.DataFrame({'Record Type': ['100', '200', '300'],
           'Value': [(1,2,3,4,5), (0,10), 1]})

def returnlist(df):
    v = []
    for i in range(df.shape[0]):
             if type(df['Value'][i]) != int:
             x = list(df['Value'][i])
             v.append(x)           
    return v

Upvotes: 1

rmutalik
rmutalik

Reputation: 2045

It looks like your 'Value' values are of string data type, so this code just maps the string value to a list of ints.

list1 = list(map(int, df['Value'][0].split(",")))

Upvotes: 6

Valentin Garreau
Valentin Garreau

Reputation: 1063

df.values.tolist() is what you are looking :)

Upvotes: -1

Related Questions