鱼C论坛

 找回密码
 立即注册
查看: 1534|回复: 3

[已解决]for循环pandas如何写入数据

[复制链接]
发表于 2022-5-27 10:57:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位老师,我需要从文本文档中提取出所有数据后将里面的数据保存到excel文件中。并且需要将蔬菜和水果分别保存在水果Sheet和蔬菜sheet页中。我尝试使用for循环可是每次只能保存最后一条数据,无法完全保存整个数据,pandas在for循环下能将所有数据保存到excel中吗?谢谢老师帮我看看。感谢
最佳答案
2022-5-27 17:15:15
  1. # 1. 获取数据
  2. df = pd.read_csv('abc.txt', encoding='GBK', sep=', ', header=None)
  3. # 考察数据 ……(略)

  4. # 2. 清洗数据
  5. import re
  6. df = df.applymap(lambda x :re.sub("['\[\]]", "", x)).reset_index()  
  7. df.columns = cols
  8. df = df.astype(dict(zip(df.columns[3:6], [float,]*3)))

  9. # 3. 输出结果
  10. df1 = df[ df['品类'] == '蔬菜'].copy().reset_index(drop=True)
  11. df2 = df[ df['品类'] == '水果'].copy().reset_index(drop=True)
  12. df1['序号'] = range(1, len(df1)+1)
  13. df2['序号'] = range(1, len(df2)+1)

  14. with pd.ExcelWriter('results.xlsx') as writer:
  15.     df1.to_excel(writer, sheet_name='蔬菜', index=False)
  16.     df2.to_excel(writer, sheet_name='水果', index=False)
复制代码

数据文本.rar

2.1 KB, 下载次数: 3

数据文件

想要达到的效果.rar

16.65 KB, 下载次数: 2

想要达到的效果

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-27 17:15:15 | 显示全部楼层    本楼为最佳答案   
  1. # 1. 获取数据
  2. df = pd.read_csv('abc.txt', encoding='GBK', sep=', ', header=None)
  3. # 考察数据 ……(略)

  4. # 2. 清洗数据
  5. import re
  6. df = df.applymap(lambda x :re.sub("['\[\]]", "", x)).reset_index()  
  7. df.columns = cols
  8. df = df.astype(dict(zip(df.columns[3:6], [float,]*3)))

  9. # 3. 输出结果
  10. df1 = df[ df['品类'] == '蔬菜'].copy().reset_index(drop=True)
  11. df2 = df[ df['品类'] == '水果'].copy().reset_index(drop=True)
  12. df1['序号'] = range(1, len(df1)+1)
  13. df2['序号'] = range(1, len(df2)+1)

  14. with pd.ExcelWriter('results.xlsx') as writer:
  15.     df1.to_excel(writer, sheet_name='蔬菜', index=False)
  16.     df2.to_excel(writer, sheet_name='水果', index=False)
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
redaiconglin + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-28 16:59:15 | 显示全部楼层

# 1. 获取数据
df = pd.read_csv('abc.txt', encoding='GBK', sep=', ', header=None)
# 考察数据 ……(略)

# 2. 清洗数据
import re
df = df.applymap(lambda x :re.sub("['\[\]]", "", x)).reset_index()  
df.columns = cols                    #老师这个位置的cols定义的是什么
df = df.astype(dict(zip(df.columns[3:6], [float,]*3)))

# 3. 输出结果
df1 = df[ df['品类'] == '蔬菜'].copy().reset_index(drop=True)
df2 = df[ df['品类'] == '水果'].copy().reset_index(drop=True)
df1['序号'] = range(1, len(df1)+1)
df2['序号'] = range(1, len(df2)+1)

with pd.ExcelWriter('results.xlsx') as writer:
    df1.to_excel(writer, sheet_name='蔬菜', index=False)
    df2.to_excel(writer, sheet_name='水果', index=False)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-28 18:59:53 | 显示全部楼层

感谢老师的讲解。非常强大的方法。谢谢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 10:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表