Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a dataframe column's list into two dataframe columns

Tags:

python

pandas

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

like image 658
ploo Avatar asked Feb 12 '26 13:02

ploo


2 Answers

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]
like image 72
root Avatar answered Feb 14 '26 03:02

root


You are close:

Instead of just splitting try this:

test2 = pd.DataFrame(test['values'].str.split('_').tolist(), columns = ['c1','c2'])
like image 36
EoinS Avatar answered Feb 14 '26 02:02

EoinS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!