How can I split a column into 2 in the correct way?

后端 未结 1 1362
小蘑菇
小蘑菇 2021-01-23 21:34

I am web-scraping tables from a website, and I am putting it to the Excel file. My goal is to split a columns into 2 columns in the correct way.

The columns what i want

相关标签:
1条回答
  • 2021-01-23 21:58

    You can use indexing with str with zfill:

    df = pd.DataFrame({'FLIGHT':['KL744','BE1013']})
    
    
    df['a'] = df['FLIGHT'].str[:2]
    df['b'] = df['FLIGHT'].str[2:].str.zfill(4)
    print (df)
       FLIGHT   a     b
    0   KL744  KL  0744
    1  BE1013  BE  1013
    

    I believe in your code need:

    df2 = pd.DataFrame(datatable,columns = cols)
    df2['a'] = df2['FLIGHT'].str[:2]
    df2['b'] = df2['FLIGHT'].str[2:].str.zfill(4)
    df2["UPLOAD_TIME"] = datetime.now()
    ...
    ...
    
    0 讨论(0)
提交回复
热议问题