Split columns without Pandas tab

Asked

Viewed 77 times

-2

Is there any pandas method for splitting columns without separator? would be a kind of excel ext.text.

I have a base with a 20-character column, but I only need the interval between 11 and 14.

ex. 22222222222ABXF22222

Obs. there is no pattern between the kernel I need.

  • Increment your question with ways you’ve tried to solve your problem

2 answers

1


Two options:

Use str and square brackets:

df['new'] = df['col'].str[11:15]

Or str.Slice:

df['new'] = df['col'].str.slice(11, 15)

Example

>>> import pandas as pd
>>> df = pd.DataFrame({"col": ["22222222222ABXF22222", "33333333333BCYG33333"]})
>>> df
                    col
0  22222222222ABXF22222
1  33333333333BCYG33333

>>> df['new'] = df['col'].str[11:15]
>>> df
                    col   new
0  22222222222ABXF22222  ABXF
1  33333333333BCYG33333  BCYG
>>>

Note: you can assign the result to the column itself ("col") and overwrite what you had previously

I hope it helps

  • Thank you very much! was spectacular!

  • @Eduardogarciadeoliveira, if you think pertinent, mark the answer as a solution and give an upvote. Take the opportunity to read this post

  • 1

    I tried, but as I am new the site returns that was computed, but does not appear as green arrow.

0

Using what the honourable Member said above, we can also simplify and put another way to exclude these unwanted numbers,

IN

>>> import pandas as pd
>>> df = pd.DataFrame({"col": ["22222222222ABXF22222", "33333333333BCYG33333"]})
>>> df['col'].replace(regex=True,inplace=True,to_replace=r'2|3',value=r'')
>>> df

OUT

col
0   ABXF
1   BCYG

Inside the TO_REPLACE you can pass several parameters and in VALUE you will be able to choose what to replace in place of the variable you are taking.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.