![](/img/trans.png)
[英]Group by to count occurrences of unique ID/itemName across multiple CSV files and sum up value in another column using pandas/python
[英]count occurrences across multiple csv files in python
我想計算唯一 email 地址的出現次數。 在每個 csv 文件中,多人可以有相同的姓氏或名字。 只有電子郵件是唯一的。 只有三列(excel:A,B,C)。
文件1.csv
|---------------------|------------------|---------------|
| A | B | C |
|---------------------|------------------|---------------|
| Last Name | First Name | Mail |
|---------------------|------------------|---------------|
| Johnson | Emma | e.j@abc.com |
|---------------------|------------------|---------------|
| Johnson | Max | m.j@abc.com |
|---------------------|------------------|---------------|
| ... | ... | ... |
|---------------------|------------------|---------------|
文件2.csv
|---------------------|------------------|---------------|
| Last Name | First Name | Mail |
|---------------------|------------------|---------------|
| Smith | Linda | l.s@abc.com|
|---------------------|------------------|---------------|
| Johnson | Max | m.j@abc.com|
|---------------------|------------------|---------------|
| ... | ... | ... |
|---------------------|------------------|---------------|
...
文件89.csv
|---------------------|------------------|---------------|
| Last Name | First Name | Mail |
|---------------------|------------------|---------------|
| Miller | Jeremy | je.m@abc.com|
|---------------------|------------------|---------------|
| Johnson | Max | m.j@abc.com|
|---------------------|------------------|---------------|
| ... | ... | ... |
|---------------------|------------------|---------------|
程序應該創建一個 output csv 文件(也可以是 .txt 文件),計算唯一郵件地址的出現次數 (>2) 並輸出名字和姓氏。 output.csv
|---------------------|------------------|---------------|---------------|
| Last Name | First Name | Mail | Occurrence |
|---------------------|------------------|---------------|---------------|
| Johnson | Max | m.j@abc.com| 3 |
|---------------------|------------------|---------------|---------------|
| ... | ... | ... | ... |
|---------------------|------------------|---------------|---------------|
可能的算法:
create new temprary csv file : temp.csv
for (i = 1; i <= 89; i++)
append content of file[i] into temp.csv
//now we have one csv file, instead of 89
count occurence of unique email in temp.csv
if occurence > 2
save column[0], column[1], column[2] and occurence
into output.csv
您可以首先在pandas
DataFrames 中加載所有 CSV:
import pandas as pd
list_of_files = ["file1.csv", ..., "file89.csv"]
dataframes = [pd.read_csv(fi) for fi in list_of_files]
然后,我們可以將所有 DataFrame 折疊成一個大 DataFrame:
big_df = pd.concat(dataframes)
那么,假設name+surname+email的組合是一致的,我們可以這樣做:
with_occ = big_df.groupby(big_df.columns.tolist()).size().reset_index().rename(columns={0:'occurrences'})
print(with_occ)
# name surname email occurrences
# 0 Kat L s@m.com 1
# 1 Max Johnson max@mail.com 2
我假設您不能一次加載 CSV 文件,話雖如此。 我閱讀了每個 CSV 文件並為 email 地址創建了一個字典,稍后。 您可以輕松地 select email 地址出現不止一次。
import os
import pandas as pd
from tqdm import tqdm
d = {}
for f in tqdm(os.listdir(CSV_FILES_DIR)):
tmp_df = pd.read_csv(f"{CSV_FILES_DIR}/{f}", index_col=0)
for e in tmp_df["email"]:
if e not in d:
d[e] = 0
else:
d[e] += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.