栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas DataFrame 写入excel是列表 读取变字符串的解决办法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas DataFrame 写入excel是列表 读取变字符串的解决办法

import pandas as pd

文章目录
    • 字典转Dataframe
    • 查看Dataframe中的数据类型
    • 写入eccel
    • 读取excel
    • 发现数据类型由列表变成了字符串,并修改数据结构
    • 修改成功

字典转Dataframe
d = {'one': [[1, 2], [3, 4], [5, 6]], 'two': [[7, 8], [9, 10], [11, 12]]}
df = pd.Dataframe(d)
df

查看Dataframe中的数据类型
for index, row in df.iterrows():
    print(type(row['one']))



写入eccel
df.to_excel(
    excel_writer=r"4.xlsx",  # 文件路径
    sheet_name='Sheet1',  # 子表的名字
    index=True,  # 是否写入index
    header=True,  # 是否写入列
    encoding="utf-8"  # 编码结构
)
读取excel
df1 = pd.read_excel(
    io=r"4.xlsx",  # 文件路径
    sheet_name='Sheet1',  # 子表名
    index_col=0,  # 用作索引的列
    header=0,  # 用列名的行
#     dtype=np.float64
)
df1
发现数据类型由列表变成了字符串,并修改数据结构
for index, row in df1.iterrows():
    print(type(row['one']))
    row['one'] = eval(row['one'])



修改成功
for index, row in df1.iterrows():
    print(type(row['one']))



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529735.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号