![](/img/trans.png)
[英]I have a large collection of small files of the same nature. Can I build dictionary on them all but compress each file individually?
[英]I have 25 .csv files (each file is a scribe) all in same structure (X, Y and STATUE). I want to combine all of them into one large .txt file
所以我尝试了这个并将所有文件(25 个抄写员文件)合并为一个。 每个划线器包含 3330 ID 号,并且有一个坐标 X 和 Y 来突出显示每个 ID 号的缺陷数量 (STATUE)。 我想知道所有组合文件中每个 ID 号的 STATUE 总和。
import os
import pandas as pd
from glob import glob
stock_files = sorted(glob('*AVI.als'))
dfList = []
stock_files
df = pd.concat((pd.read_csv(file).assign(filename = file) for file in stock_files), ignore_index = True)
X\tY\tSTATUS filename
0 14\t1\t0 2008-09728-AVI.als
1 15\t1\t0 2008-09728-AVI.als
2 16\t1\t0 2008-09728-AVI.als
3 17\t1\t0 2008-09728-AVI.als
4 18\t1\t0 2008-09728-AVI.als
... ... ...
83245 30\t90\t0 2008-13754-AVI.als
83246 31\t90\t0 2008-13754-AVI.als
83247 32\t90\t0 2008-13754-AVI.als
83248 33\t90\t0 2008-13754-AVI.als
83249 34\t90\t0 2008-13754-AVI.als
对于所有 CSV 文件合并到一个 .txt 文件中,我应该看到如下结果
X Y STATUS
0 14 1 0
1 15 1 0
2 16 1 0
3 17 1 0
4 18 1 0
...
3330
任何帮助深表感谢
我认为您只需要添加分隔符(sep=r"\t"):
df = pd.concat([pd.read_csv(file, sep=r"\t").assign(filename = file) for file in stock_files], ignore_index = True)
您可以像这样简单地保存到.txt:
df.to_csv("output.txt")
如果您想要每个 ID (X) 的 STATUS 总和,您可以这样做:
df.groupby(["X"])["STATUS"].sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.