python,有没有更简单的方法来分割数据点?

时间:2019-04-23 20:43:32

标签: pandas

我想让它分析将有1000多个行的数据(我只是用20个行对其进行测试,直到完成),我想进行18个部分,而在每18个部分中,我想查找3列的平均值每个部分。这是我开始做的方式。


import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt


ver = '''2018.12.0400:00,0.73572,0.73614,0.73544,0.73550,520,0
2018.12.0401:00,0.73550,0.73594,0.73545,0.73553,1181,0
2018.12.0402:00,0.73553,0.73606,0.73510,0.73539,1960,0
2018.12.0403:00,0.73539,0.73621,0.73481,0.73608,2898,0'''

ver = [i.split(',') for i in ver.split('\n')]

df = pd.DataFrame(ver)
print(df)

NoR = 4
'''
NoR is number of rows, this number will change for 
every data set and will have to be ajusted for most accurate reading
'''

df[1] = df[1].astype(float)
df[2] = df[2].astype(float)
df[3] = df[3].astype(float)
df[4] = df[4].astype(float)
df[5] = df[5].astype(float)

def small_sections(al,ah,av):

    S1S = NoR * (1/18)
    #wholenumberS1S = round(S1S)
    #these are the individule secionts that will be analized
    S2S = S1S + S1S
    S3S = S2S + S1S
    S4S = S3S + S1S
    S5S = S4S + S1S
    S6S = S5S + S1S
    S7S = S6S + S1S
    S8S = S7S + S1S
    S9S = S8S + S1S
    S10S = S9S + S1S
    S11S = S10S + S1S
    S12S = S11S + S1S
    S13S = S12S + S1S
    S14S = S13S + S1S
    S15S = S14S + S1S
    S16S = S15S + S1S
    S17S = S16S + S1S
    S18S = S17S + S1S + 1000 #overkill but in case rounding causes it to not calc the last few data points

    #average high df[2]
    MoH1 = df.loc[0:S1S, 2].mean()
    MoH2 = df.loc[S1S:S2S, 2].mean()
    MoH3 = df.loc[S2S:S3S, 2].mean()
    MoH4 = df.loc[S3S:S4S, 2].mean()
    MoH5 = df.loc[S4S:S5S, 2].mean()
    MoH6 = df.loc[S5S:S6S, 2].mean()
    MoH7 = df.loc[S6S:S7S, 2].mean()
    MoH8 = df.loc[S7S:S8S, 2].mean()
    MoH9 = df.loc[S8S:S9S, 2].mean()
    MoH10 = df.loc[S9S:S10S, 2].mean()
    MoH11 = df.loc[S10S:S11S, 2].mean()
    MoH12 = df.loc[S11S:S12S, 2].mean()
    MoH13 = df.loc[S12S:S13S, 2].mean()
    MoH14 = df.loc[S13S:S14S, 2].mean()
    MoH15 = df.loc[S14S:S15S, 2].mean()
    MoH16 = df.loc[S15S:S16S, 2].mean()
    MoH17 = df.loc[S16S:S17S, 2].mean()
    MoH18 = df.loc[S17S:S18S, 2].mean()

所以我让第一列做了很多复制,并为接下来的2列粘贴相同的内容。这是最简单的方法还是还有另一种方法?

0 个答案:

没有答案