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

前端 未结 5 1021
青春惊慌失措
青春惊慌失措 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 09:05

    Combining the answers by @EdChum and @Yu Zhou, a simpler solution would be:

    for col, dtype in df.dtypes.items():
        if dtype == np.object:  # Only process byte object columns.
            df[col] = df[col].apply(lambda x: x.decode("utf-8"))
    

提交回复
热议问题