[Python] How to Split a column into two by comma delimiter, and put a value without comma in...

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    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)

    Continue reading...

Compartilhe esta Página