python合并csv文件、并利用结果进行计算_循环浏览csv文件并将计算结果输出到一个合并的csv fi...

我正在尝试遍历一个指定的文件夹,其中包含一堆.csv文件。其目的是收集每个文件中列出的三个指标,将它们相加,并将其输出到一个合并的行中,该行很快将由python csv文件生成。在

这是我的代码:import pandas as pd

import os

results_output = pd.DataFrame(index = None, columns=["gross_revenue","impressions","bid_requests"])

filenames = os.listdir("Z:/some/folder/path")

count = 0

for file in filenames:

file_path = "Z:/some/folder/path/" + file

data = pd.read_csv(file_path,skiprows=[0])

data.columns = ["product1_gross_revenue","product2_gross_revenue","product3_gross_revenue"]

gross_revenue = (data.product1_gross_revenue[0] + data.product2_gross_revenue[0] + data.product3_gross_revenue[0])

if gross_revenue > 0:

count += 1

results_output.gross_revenue[1+count] = [gross_revenue]

results_output.to_csv("data.csv",index=False,header=False)

这是错误:

^{pr2}$

我可以在控制台上打印出所有的收入项目,这样我就知道它通过每个文件正确地计算,并且数据.csv“文件已生成,但仍为0kb,没有写入任何内容,因此我怀疑这是我犯错误的地方。在

这就是我想要的”数据.csv“文件外观:0 gross_revenue[file1]

1 gross_revenue[file2]

2 gross_revenue[file3]

3 gross_revenue[file4]

4 gross_revenue[file5]

5 gross_revenue[file6]

6 gross_revenue[file7]

希望我已经提供了所有有用的信息,很乐意提供任何其他信息。在

提前谢谢!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我不能在这里直接生成文件。但是,我可以提供一个Python代码示例,您可以使用它来生成所需的CSV文件。 ```python import csv import random # List of possible values for each field names = ['Naruto', 'One Piece', 'Dragon Ball', 'Attack on Titan', 'Death Note', 'Fullmetal Alchemist', 'Sailor Moon', 'Bleach', 'Hunter x Hunter', 'My Hero Academia'] genres = ['Action', 'Adventure', 'Comedy', 'Drama', 'Fantasy', 'Horror', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller'] types = ['TV', 'OVA', 'Movie', 'ONA'] episodes = [12, 24, 26, 50, 100] ratings = [8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9.0] members = [100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000] descriptions = ['A young ninja strives to become the strongest in his village.', 'Pirates search for the ultimate treasure.', 'A boy with incredible powers fights to protect the world.', 'Humanity battles against giant man-eating Titans.', 'A student gains the power to kill anyone whose name he writes in a notebook.', 'Two brothers seek a way to restore their bodies after a failed alchemical experiment.', 'A group of teenage girls transform into magical warriors to defend the Earth.', 'A teenager gains the ability to see ghosts and becomes a Soul Reaper.', 'A boy follows in his father\'s footsteps and becomes a Hunter.', 'A boy without powers dreams of becoming a hero in a world where everyone has them.'] pictures = ['https://example.com/naruto.jpg', 'https://example.com/onepiece.jpg', 'https://example.com/dragonball.jpg', 'https://example.com/aot.jpg', 'https://example.com/deathnote.jpg', 'https://example.com/fma.jpg', 'https://example.com/sailormoon.jpg', 'https://example.com/bleach.jpg', 'https://example.com/hxh.jpg', 'https://example.com/mha.jpg'] # Generate 1000 rows of data rows = [] for i in range(1000): row = [random.choice(names), random.choice(genres), random.choice(types), random.choice(episodes), random.choice(ratings), random.choice(members), random.choice(descriptions), random.choice(pictures)] rows.append(row) # Write data to CSV file with open('anime_data.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['name', 'genre', 'type', 'episodes', 'rating', 'members', 'description', 'picture']) writer.writerows(rows) ``` 这段代码将生成一个包含1000行的CSV文件,其中每行都包含“name”,“genre”,“type”,“episodes”,“rating”,“members”,“description”和“picture”字段的值。您可以根据需要修改列表中的值或添加/删除字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值