I've been wrapping my head around this problem for some days now:
- I have several dataframes in csv files which have all the same format (column names, row names), and I want to calculate the mean of each row of each file, then save it in an output csv file. And that works like this:
import glob
import csv
with open('result.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
for filename in glob.glob('F*.csv'):
print (filename)
with open (filename, newline='') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
averages = [] #making a list for averages
for col in zip(*csv_input):
averages.append(sum(float(x) for (x) in col) / len(col))
csv_output.writerow([filename] + averages)
- But I actually need the mean of each colum of each dataframe every 18 rows (because every row = 1 minute) excluding the header (beacuse that's a string). So I've been trying to select only the first 18 rows with something like this in the for loop
df = pd.read_csv(df,skiprows=0, nrows = 18) #to get only first 18 minutes'
But this is 1)not working (I think beacuse it's not applicable to a reader) 2)not ideal beacuse I actually just want to divide all the dfs in chunks of 18 rows and then calculate the mean of each row.
Any hint would be really helpful, thanks!
update
"Maybe you can show us a small sample dataframe (with 4-5 columns and rows) and show us what output you want (pretending you want it every 2 rows instead of every 18)."
This is my sample df for each file
col1 col2 col3 col4 col5 col6
0 1.228516 61.228516 1.0 418.808284 957.419867 1025.554374
1 61.228516 121.228516 1.0 207.864712 694.581742 814.149004
2 121.228516 181.228516 1.0 140.516367 370.653176 809.100268
3 181.228516 241.228516 1.0 140.274964 448.755593 885.196647
4 241.228516 301.228516 1.0 117.079110 312.173256 1907.873698
I wnat to calculate the mean of the first two rows (0,1) for each colum, let's say here forl col6. So my output would be (1025.554374+814.149004)/2 #definition of arithmetic mean.
I just need this in all my csv files but for every 18 rows
Hope it's clearer now, thanks!