通过pandas计算每列中每10个单元格的平均值
Calculating the average value for every 10 cells in each column by pandas
在我的 excel csv 文件中,大约有 1500 行和 30 列。我相信我可以用 python 来完成它。所以这是我的目标:
- 如何让 python 正确读取我的 excel 文件。
- 我想将行数减少到1/10,那么如何计算每列每10行的平均值?
- 同时,我希望保留timeslot栏,以便我了解它表示的时间段。
这是我的 excel 文件的简称。
enter image description here
文件已上传到Google盘,请试看
https://drive.google.com/file/d/1EDmSgsEoNQYZeRD_JiR33WNv7ENW4cp3/view?usp=sharing
我使用的代码如下所示
import numpy as np
import pandas as pd
import glob
location='C:\Users\Poon\Downloads\20211014_SBS_BEMS\20211014_SBS_BEMS\1043 succeed.csv'
csvfiles=glob.glob(location)
df1=pd.DataFrame()
for file_new_2 in csvfiles:
df2=pd.read_csv(file_new_2)
df1=pd.concat([df1,df2],ignore_index=True)
df1.mean(axis=0)#average for each column
df1.mean(axis=1)
n = 100 # the number of rows
df1.groupby(np.arange(len(df1))//n).mean()
print(df1)
此代码将清理您的数据并取每 10 行的平均值。
df = df.iloc[1:, :]
df = pd.concat([pd.to_datetime(df.iloc[:,0], errors = "coerce"), df.iloc[:, 1:].apply(pd.to_numeric)], axis = 1)
df.dropna(inplace = True)
df["index"] = df.index//10
df.groupby("index").mean()
在我的 excel csv 文件中,大约有 1500 行和 30 列。我相信我可以用 python 来完成它。所以这是我的目标:
- 如何让 python 正确读取我的 excel 文件。
- 我想将行数减少到1/10,那么如何计算每列每10行的平均值?
- 同时,我希望保留timeslot栏,以便我了解它表示的时间段。
这是我的 excel 文件的简称。 enter image description here
文件已上传到Google盘,请试看 https://drive.google.com/file/d/1EDmSgsEoNQYZeRD_JiR33WNv7ENW4cp3/view?usp=sharing
我使用的代码如下所示
import numpy as np
import pandas as pd
import glob
location='C:\Users\Poon\Downloads\20211014_SBS_BEMS\20211014_SBS_BEMS\1043 succeed.csv'
csvfiles=glob.glob(location)
df1=pd.DataFrame()
for file_new_2 in csvfiles:
df2=pd.read_csv(file_new_2)
df1=pd.concat([df1,df2],ignore_index=True)
df1.mean(axis=0)#average for each column
df1.mean(axis=1)
n = 100 # the number of rows
df1.groupby(np.arange(len(df1))//n).mean()
print(df1)
此代码将清理您的数据并取每 10 行的平均值。
df = df.iloc[1:, :]
df = pd.concat([pd.to_datetime(df.iloc[:,0], errors = "coerce"), df.iloc[:, 1:].apply(pd.to_numeric)], axis = 1)
df.dropna(inplace = True)
df["index"] = df.index//10
df.groupby("index").mean()