I have a column in a df that I want to split into two columns splitting by comma delimiter. If the value in that column does not have a comma I want to put that into the second column instead of first.
| Origin |
|---|
| New York, USA |
| England |
| Russia |
| London, England |
| California, USA |
| USA |
I want the result to be:
| Location | Country |
|---|---|
| New York | USA |
| NaN | England |
| NaN | Russia |
| London | England |
| California | USA |
| NaN | USA |
I used this code
df['Location'], df['Country'] = df['Origin'].str.split(',', 1)
>Solution :
We can try using str.extract here:
df["Location"] = df["Origin"].str.extract(r'(.*),')
df["Country"] = df["Origin"].str.extract(r'(\w+(?: \w+)*)$')