Pandas add new columns based on splitting another column

前端 未结 3 1808
失恋的感觉
失恋的感觉 2021-01-03 04:42

I have a pandas dataframe like the following:

A              B
US,65,AMAZON   2016
US,65,EBAY     2016

My goal is to get to look like this:

3条回答
  •  生来不讨喜
    2021-01-03 05:25

    For getting the new columns I would prefer doing it as following:

    df['Country'] = df['A'].apply(lambda x: x[0])
    df['Code'] = df['A'].apply(lambda x: x[1])
    df['Com'] = df['A'].apply(lambda x: x[2])
    

    As for the replacement of , with a . you can use the following:

    df['A'] = df['A'].str.replace(',','.')
    

提交回复
热议问题