-1

I have a column as below:

       A
0   Australia
1   New Zealand
2   New Jersey,America
3   Hyderabad,India

I want to split it in two columns where there is ',' separator such as:

        A                  B
0   Australia             NaN
1   New Zealand           NaN
2   New Jersey,America  America
3   Hyderabad,India      India

Any suggestion is welcomed

Abdul Quddus
  • 111
  • 2
  • 10

2 Answers2

1

This should help.

df["Country"] = df.City.str.split(',', expand=True )[1]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

You can try this:

def splitter(x):
   try: 
      y = x.split(",")[1]
   except:
      y = None
   return y
df["B"] = df["A"].apply(splitter)
edyvedy13
  • 2,156
  • 4
  • 17
  • 39