0

I did the following:

df[['col1','col2']] = df.col.str.split(',', expand = True)

It works well for like, "a,b". If the col value is a string without a comma like "a", the above line takes that string to the col1. But if I want to take that in the second column that is, col2, is there any one-line way to do that?

Note that: I want to work it reversely only when it gets a string without a comma.

For more clarification: Sample dataframe column,

col: "a,b", "a"

Expected output,

row 1-> col1: a, col2: b

row 2-> col1: none, col2: a

Thanks in advance :))

Abu Ubaida
  • 95
  • 6

1 Answers1

2

You should rather use extract instead of split:

df.col.str.extract(r'(\w*),?(\w+)')

   0  1
0  a  b
1     a

where df = pd.DataFrame({'col':['a,b', 'a']})

Note that the regex can change with respect to what is needed

EDIT:

 df.col.str.extract(r'^([^,]*),?(\b\w+)')

        0         1
0  Uttara     Dhaka
1          Faridpur
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • The above is working fine for that example. What will be the regex, if 'a' is city name and, 'b' is the name of state? – Abu Ubaida May 14 '21 at 09:02
  • @AbuUbaida you will have to give examples – Onyambu May 14 '21 at 09:38
  • suppose, `df = pd.DataFrame({'col':['Uttara,Dhaka', 'Faridpur']})` . I want output in this format: **row 1->** _col1: Uttara, col2: Dhaka_, **row 2->** _col1: none, col2: Faridpur_. For your convenience, you can consider **col1** and **col2** values to be city and state names respectively. So it can be any valid names. :)) Thanks for your time. – Abu Ubaida May 14 '21 at 13:55
  • @AbuUbaida in my code above, where there is `\q` change that to `[^,]` – Onyambu May 14 '21 at 17:40
  • I am not seeing any `\q` in your code `df.col.str.extract(r'(\w*),?(\w+)')`. Did you mean `\w`? If so, this `df.col.str.extract(r'([^,]*),?([^,]+)')` is working as like the previous one. **row 2->** is showing `col1: Faridpu, col2: r` in both of the cases. – Abu Ubaida May 14 '21 at 18:06
  • @AbuUbaida yes sorry meant to say `\w` please use `r'^([^,]*),?([^,]+)'` – Onyambu May 14 '21 at 20:17
  • tried that also. but it's been arisen the same problem I mentioned in the earlier comment – Abu Ubaida May 15 '21 at 04:03