简体   繁体   中英

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

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[0][0] 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[0][0])

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[0][0], sep='\n')
                  probabilities
0  [0.001, 0.002, 0.003, 0.004]
1  [0.005, 0.006, 0.007, 0.008]
0.001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM