# Convert a string with numbers into a list of integers using Python

## Question:

I’ve a dataframe column with an string of numbers and I want to convert it into a list of numbers. The output must be a list, since I need be able to pull the index value (for instance, df.probabilities and return 0.001).

This my current dataframe:

`````` probabilities
0.001, 0.002, 0.003, 0.004
``````

I need this:

``````  probabilities
[0.001, 0.002, 0.003, 0.004]
``````

Thank you in advance.

You can call str.split and `float` using a list comprehension in `DataFrame.apply`:

``````
import pandas as pd

def parse_probabilities(string):
return [float(value) for value in string.split(',')]

df = pd.DataFrame({'probabilities': ['0.001, 0.002, 0.003, 0.004']})

df['probabilities'] = df['probabilities'].apply(parse_probabilities)

print(df)
print(df.probabilities)

``````

Uses .str accessor with split:

``````df['probabilities'].str.split(',s?')
``````
``````df = pd.DataFrame({'probabilities': ['0.001, 0.002, 0.003, 0.004', '0.005, 0.006, 0.007, 0.008']})
df.probabilities = df.probabilities.str.split(',', expand=True).astype(float).apply(list, axis=1)
print(df, df.probabilities, sep='n')
``````
``````                  probabilities
0  [0.001, 0.002, 0.003, 0.004]
1  [0.005, 0.006, 0.007, 0.008]
0.001
``````
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.