0

I have this column in my dataframe which has numbers in a string like "6,22,67,82" for example. I want to split this strings into arrays of integers and keep the arrays in the dataframe.

  h['htgt']=h['htgt'].split()

This does not work because it tries to split the whole series.

1 Answer 1

1

You can use pd.Series.str.split with expand=True and then convert to int. Assumes you have an equal number of numbers in each string.

h = pd.DataFrame({'htgt': ['6,22,67,82', '12,45,65,14', '54,15,9,94']})

res = h['htgt'].str.split(',', expand=True).astype(int)

print(res)

    0   1   2   3
0   6  22  67  82
1  12  45  65  14
2  54  15   9  94
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. The strings do not contain the same amount of numbers, so I am using expand is False, to keep array as entries in the column.

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.