Home > Back-end >  split a string values in column using python
split a string values in column using python

Time:10-22

Consider a sample dataframe

df11 = pd.DataFrame({'A': ['1' '_' '2']})

I want to split column values to list in the respective rows and convert into integer. I am using the below code, the output values are in the form of string. How can i convert into integer

df11["Timetaken"] = df11['A'].str.split('_')

current output column i have is like below

enter image description here

I want to convert the string of values to integer or float

CodePudding user response:

Using list comprehension, simply add:

[int(x) for x in df11['A'].str.split('_')]

This will give you a list of integers.


For the edited question, you can change the type of those values using

df11['Timetaken'] = df11['Timetaken'].apply(lambda x: [int(y) for y in x])

CodePudding user response:

You can use expand=True then use astype and use agg like below:

As int

>>> df['Timetaken'] =  df11['A'].str.split('_', expand=True).astype(int).apply(lambda x: x.dropna().tolist(), axis=1)

As float

>>> df['Timetaken'] =  df11['A'].str.split('_', expand=True).astype(float).apply(lambda x: x.dropna().tolist(), axis=1)

>>> df
    Timetaken
0   [9.0, 6.0, 36.0]
1   [3.0, 1.0]
2   [9.0, 2.0]
3   [6856.0, 4870.0]
4   [6864.0]
5   [6873.0] 

CodePudding user response:

You can use only pandas methods like so:

df = pd.DataFrame(
    {
        "Timetaken": [
            "9.0_6.0_36.0",
            "3.0_1.0",
            "9.0_2.0",
            "6856.0_4870.0",
            "6864.0",
            "6873.0",
        ]
    }
)

df["Timetaken"] = (
    df["Timetaken"]
    .str.split("_")
    .explode()
    .astype("float")
    .astype("int")
    .groupby(level=0)
    .apply(list)
)

Which will:

  • split the strings
  • Explode them into separate rows
  • Convert them to numeric (.astype('float').astype('int'))
  • finally put them back into lists (.groupby(...).apply(...))
  • Related