![](/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.