0
点赞
收藏
分享

微信扫一扫

df list 转 str object astype

写心之所想 2024-04-25 阅读 13

import pandas as pd

df = pd.DataFrame()
df["lis"] = [[1, 2], [3, 4, 5]]

# print(df.dtypes)  # object 表示混合类型
# print(df.astype(str).dtypes)  # 仍然object 表示str
# print(type(df.astype(str).iat[0, 0]))  # str
# df["str"] = "string"  # 在已有shape上赋值
# print(df["str"].dtypes)  # object 表示str
# 结论 astype(str)有效?
# print(type(df["lis"].astype(object).iat[0]))  # list
print(type(df["lis"].astype(str).iat[0]))  # str
# 结论 必须astype(str) 或 apply(str)
print(type(df["lis"].apply(str).iat[0]))
print(df["lis"].apply(str).dtypes)  # object 表示str

举报

相关推荐

0 条评论