-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
  • 91
  • 2
  • 9

2 Answers2

1

This should help.

df["Country"] = df.City.str.split(',', expand=True )[1]
Rakesh
  • 78,594
  • 17
  • 67
  • 103
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
  • 1,926
  • 2
  • 14
  • 37