How to translate “bytes” objects into literal strings in pandas Dataframe, Python3.x?

前端 未结 5 1023
青春惊慌失措
青春惊慌失措 2020-11-29 08:32

I have a Python3.x pandas DataFrame whereby certain columns are strings which as expressed as bytes (like in Python2.x)

import pandas as pd
df = pd.DataFrame         


        
5条回答
  •  难免孤独
    2020-11-29 08:46

    You can use vectorised str.decode to decode byte strings into ordinary strings:

    df['COLUMN1'].str.decode("utf-8")
    

    To do this for multiple columns you can select just the str columns:

    str_df = df.select_dtypes([np.object])
    

    convert all of them:

    str_df = str_df.stack().str.decode('utf-8').unstack()
    

    You can then swap out converted cols with the original df cols:

    for col in str_df:
        df[col] = str_df[col]
    

提交回复
热议问题