[英]Python total sum from a csv file
我有 133 個 CSV 文件
第一個文件 file1.cvs 有以下數據:
A b C
Name 2 Value
jack 3 2%
jack 3 1.33%
jack 4 1.112%
sara 5 4%
sara 6 9%
adam 1 7%
adam 2 10%
nada 3 3%
nada 4 1%
tom 5 1%
我想計算列(僅限 jack、sara、tom)和 C 列上特定名稱的總和,並將輸出保存在新的 csv 文件中,如下所示:
File name : file1.csv
jack 4.442%
sara 13%
tom 1%
File name : file2.csv
.......等等
使用任何編程語言( python 、 ruby 、 r 等..)
1.創建可重現的最小示例數據
df <- data.frame(A=rep(c("Jack", "Joe"), 3), C=runif(6))
2.使用dplyr
庫的解決方案:
library(dplyr)
summarised <- df %>%
group_by(A) %>%
summarise(Total = sum(C))
write.csv(summarised, "File_Name.csv")
使用R
你可以先搜索所有包含在一個文件夾中的CSV文件,然后做一個sapply
超過該矢量(使用dplyr
包來執行所需的操作)。 最后,在list.files
指示的同一文件夾中搜索結果文件。
library(dplyr)
#Find all the csv files in the indicated path
#Change the path location to the folder where you have your csv files
file_locs<-list.files(path="C:/Folder with csvs",
pattern = ".csv",
full.names = T)
sapply(file_locs, function(x){
#Read csv, skipping first line if it contains the A, b, c entries
#as headers, if not you can remove the "skip = 1"
df<-read.csv(x, skip = 1)
#Use dplyr to get the Value sum, grouped by Name
resuls<-df %>%
group_by(Name) %>%
summarize(sumVal = sum(Value))
#Get the csv original name, i.e., without the .csv part
file_name<-strsplit(x,".csv")[[1]][1]
#Write the results using the original file name and adding: _resul
write.csv(resuls, paste0(file_name,"_resul.csv"),row.names = F)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.