3

I'm effectively trying to do a text-to-columns (from MS Excel) action, but in Pandas.

I have a dataframe that contains values like: 1_1, 2_1, 3_1, and I only want to take the values to the right of the underscore. I figured out how to split the string, which gives me a list of the broken up string, but I don't know how to break that out into different dataframe columns.

Here is my code:

import pandas as pd

test = pd.DataFrame(['1_1','2_1','3_1'])
test.columns = ['values']

test = test['values'].str.split('_')

I get something like: [1, 1], [2, 1], [3, 1].

What I'm trying to get is two separate columns:

col1: 1, 2, 3 col2: 1, 1 ,1

Thoughts? Thanks in advance for your help

2 Answers 2

2

Use expand=True when doing the split to get multiple columns:

test['values'].str.split('_', expand=True)

If there's only one underscore, and you only care about the value to the right, you could use:

test['values'].str.split('_').str[1]
Sign up to request clarification or add additional context in comments.

Comments

2

You are close:

Instead of just splitting try this:

test2 = pd.DataFrame(test['values'].str.split('_').tolist(), columns = ['c1','c2'])

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.