Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a section of a string from one column and putting it into a new pandas column

I'm cleaning a dataset and need to take the part of the string between the underscores (_). Column A is what I am starting with.

A   
foo_bar_foo
bar_foo_bar
bar
foo_bar_foo

I need to copy over the characters in between the underscores and copy them into a new column. Column B is the anticipated results.

A               B
foo_bar_foo     bar
bar_foo_bar     foo
bar             null
foo_bar_foo     bar

Any advice is appreciated.

like image 389
Mitchell.Laferla Avatar asked Dec 03 '20 20:12

Mitchell.Laferla


People also ask

How do I split a string into another column in pandas?

split() Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string.

How to split a string column in a pandas Dataframe?

You can use the following basic syntax to split a string column in a pandas DataFrame into multiple columns: #split column A into two columns: column A and column B df[[' A ', ' B ']] = df[' A ']. str. split (', ', 1, expand= True) The following examples show how to use this syntax in practice. Example 1: Split Column by Comma

How do you copy a row and insert a column in Excel?

Copy & Insert Row / Column Instead you can insert the copied row or column and shift the existing rows or columns to make room. This will copy row 1 and insert it into row 5, shifting the existing rows down: Range ("1:1").Copy Range ("5:5").Insert

How to copy one string to another string in C++?

Using the inbuilt function strcpy () from string.h header file to copy one string to the other. strcpy () accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string.

How do I paste over an existing row or column?

When pasting rows and columns you have two options: You can paste over the existing row (or column) or you can insert a new row (or column). Let’s look at the difference… Copy & Paste Over Existing Row / Column This will copy row 1 and paste it into the existing row 5:


2 Answers

Use str.split and .str[index]

df['B']=df.A.str.split('_').str[1]



      A        B
0  foo_bar_foo  bar
1  bar_foo_bar  foo
2          bar  NaN
3  foo_bar_foo  bar
like image 69
wwnde Avatar answered Nov 14 '22 23:11

wwnde


Use extract:

df['B'] = df['A'].str.extract('_(\w+)_')
print(df)

Output

             A    B
0  foo_bar_foo  bar
1  bar_foo_bar  foo
2          bar  NaN
3  foo_bar_foo  bar
like image 40
Dani Mesejo Avatar answered Nov 15 '22 01:11

Dani Mesejo