1

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

2
  • What is the expected output? Commented Oct 22, 2021 at 11:44
  • 1
    i have added a snippet of output i have. all i want is to convert the string of values inside list in each row to integer/float Commented Oct 22, 2021 at 11:46

3 Answers 3

2

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])
Sign up to request clarification or add additional context in comments.

2 Comments

i have a huge dataframe with column whose value is a number joined in the formed of string. how can i add use your answer to convert entire column into comma seperated list of integers
@SoKu Please see edited answer.
1

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] 

Comments

0

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(...))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.