How to divide the value of an element of a column by delimiter (p.e "|") in pandas?

Asked

Viewed 258 times

2

Let’s say we have the following column:

coluna1
ola | 52
   hey
   sou
   ja
 da |5
  24g

The expected output would be:

coluna1
   ola
    52
    hey
    sou
     ja
     da
     5
     24g

So far, I’m trying string manipulation with split and rsplit and I still can’t get the expected result...as p.e

 df.coluna1.str.split("|")

Update:

coluna1
ola | 52 | 55 |23
   hey - e| 10 |12
   sou
   ja
  da |5 |ola
   24g

Expected output:

coluna1
   ola
    52
    55
    23
    hey - e
    10
    12
    sou
     ja
     da
     5
     ola
     24g

In this case only the split of the first | that appears ... in the others not!

  • all solved with the first method!

2 answers

1

Opa,

You can do as below:

Creating the Dataframe

>>> import pandas as pd

>>> df = pd.DataFrame({"coluna1": ["ola|52", "hey", "sou", "ja", "da|5", "24g"]})

>>> df

>>> df
  coluna1
0  ola|52
1     hey
2     sou
3      ja
4    da|5
5     24g

Doing the "split"

>>> pd.concat([pd.Series(row['coluna1'].split('|')) for _, row in df.iterrows()]).reset_index()

   index    0
0      0  ola
1      1   52
2      0  hey
3      0  sou
4      0   ja
5      0   da
6      1    5
7      0  24g

Notice that the column index shows the position of the string.

I hope I’ve helped.

1


One way to do this is by using the function explode of own Pandas:

df = pd.DataFrame(df['coluna1'].str.split('|').explode().reset_index(drop = True))

Entree:

    coluna1
0   ola|52
1   hey
2   sou
3   ja
4   da|5
5   24g

Exit:

    coluna1
0   ola
1   52
2   hey
3   sou
4   ja
5   da
6   5
7   24g

Description:

Transform each element of a list into a row by replicating the index values.


df = pd.DataFrame(df['coluna1'].str.split('|').explode().reset_index(drop = True))

New entry:

coluna1
ola | 52 | 55 |23
   hey - e| 10 |12
   sou
   ja
  da |5 |ola
   24g

Exit:

coluna1
0   ola
1   52
2   55
3   23
4   hey - e
5   10
6   12
7   sou
8   ja
9   da
10  5
11  ola
12  24g
  
  • thanks @Imonferrari...but what if you have several | No longer works...

  • Zoramind, good afternoon! In which case it doesn’t work?

  • I’ll edit that maybe it’s easier to explain! Thank you!

  • But the Solution I gave you works for this new case....

  • See update, it’s the same code, now assigns the command to variable df

  • Thank you very much! I was repeating and it wasn’t worth it... Perfect!

  • Not at all! I’m glad you did. Hugs!

Show 2 more comments

Browser other questions tagged

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