簡體   English   中英

Pandas pivot_table 中的函數參數

[英]Function arguments within Pandas pivot_table

我在 Pandas 數據框中有風向和速度數據,以 10 分鍾為單位。 它看起來像這樣:

      year  month  day  hour  minutes  direction  speed        filename
0   1999.0      1    1     0        0       84.0    7.1  mlrf1c1999.txt
1   1999.0      1    1     0       10       75.0    7.5  mlrf1c1999.txt
2   1999.0      1    1     0       20       79.0    7.2  mlrf1c1999.txt
3   1999.0      1    1     0       30       77.0    7.2  mlrf1c1999.txt
4   1999.0      1    1     0       40       76.0    6.7  mlrf1c1999.txt
5   1999.0      1    1     0       50       76.0    7.5  mlrf1c1999.txt
6   1999.0      1    1     1        0       81.0    6.9  mlrf1c1999.txt
7   1999.0      1    1     1       10       75.0    7.3  mlrf1c1999.txt
8   1999.0      1    1     1       20       77.0    7.4  mlrf1c1999.txt
9   1999.0      1    1     1       30       73.0    6.9  mlrf1c1999.txt
10  1999.0      1    1     1       40       78.0    6.5  mlrf1c1999.txt
11  1999.0      1    1     1       50       75.0    7.3  mlrf1c1999.txt
...
1147812  1997.0     12   31    21        0      261.0    6.0  mlrf1c1997.txt
1147813  1997.0     12   31    21       10      260.0    5.9  mlrf1c1997.txt
1147814  1997.0     12   31    21       20      262.0    5.5  mlrf1c1997.txt
1147815  1997.0     12   31    21       30      279.0    6.5  mlrf1c1997.txt
1147816  1997.0     12   31    21       40      283.0    7.3  mlrf1c1997.txt
1147817  1997.0     12   31    21       50      282.0    7.2  mlrf1c1997.txt
1147818  1997.0     12   31    22        0      277.0    6.9  mlrf1c1997.txt
1147819  1997.0     12   31    22       10      283.0    7.6  mlrf1c1997.txt
1147820  1997.0     12   31    22       20      283.0    7.2  mlrf1c1997.txt
1147821  1997.0     12   31    22       30      290.0    7.5  mlrf1c1997.txt
1147822  1997.0     12   31    22       40      289.0    7.2  mlrf1c1997.txt
1147823  1997.0     12   31    22       50      292.0    7.6  mlrf1c1997.txt
1147824  1997.0     12   31    23        0      296.0    7.7  mlrf1c1997.txt

我正在嘗試使用數據透視表檢查數據,以便獲得每小時切片的平均方向和速度。 我需要將 Scipy 的 circmean 函數應用於方向數據。 這需要為數據集指定高參數和低參數。 當我嘗試這樣做時,我得到一個 TypeError: 'numpy.float64' object is not callable。

df.pivot_table(values = ['direction'], index = ['day', 'hour'], aggfunc = circmean(df.direction, high=df.direction.max(), low=df.direction.min()))

df.pivot_table(values = ['direction'], index = ['day', 'hour'], aggfunc = circmean(df.direction, high=360, low=0))

據我了解,circmean 需要高參數和低參數才能獲得准確的輸出。 當我嘗試使用 np.mean 獲取風速讀數的平均值時,我沒有任何困難:

df.pivot_table(values = ['speed'], index = ['day', 'hour'], aggfunc = np.mean)

其中產生:

             speed
day hour          
1   0     6.085055
    1     6.144919
    2     6.253006
    3     6.315291
    4     6.305656
    5     6.241176
    6     6.205701

我還可以應用不帶參數的 circmean 函數,如下所示:

df.pivot_table(values = ['direction'], index = ['day', 'hour'], aggfunc = circmean)

當我這樣做時,我得到了我無法解釋的結果(也就是說,它們不是 360 度):

          direction
day hour           
1   0      2.992024
    1      3.414254
    2      1.620715
    3      0.463309
    4      6.206874
    5      1.451950
    6      4.319550

有沒有辦法在 pivot_table 的 aggfunc 參數中應用函數和參數? 如果沒有,是否有人建議我如何從數據框中獲取我需要的通函?

這是一些復制您的問題的代碼:

import io
import pandas as pd
from scipy.stats import circmean

doc = """      year  month  day  hour  minutes  direction  speed        filename
0   1999.0      1    1     0        0       84.0    7.1  mlrf1c1999.txt
1   1999.0      1    1     0       10       75.0    7.5  mlrf1c1999.txt
2   1999.0      1    1     0       20       79.0    7.2  mlrf1c1999.txt
3   1999.0      1    1     0       30       77.0    7.2  mlrf1c1999.txt
4   1999.0      1    1     0       40       76.0    6.7  mlrf1c1999.txt
5   1999.0      1    1     0       50       76.0    7.5  mlrf1c1999.txt
6   1999.0      1    1     1        0       81.0    6.9  mlrf1c1999.txt
7   1999.0      1    1     1       10       75.0    7.3  mlrf1c1999.txt
8   1999.0      1    1     1       20       77.0    7.4  mlrf1c1999.txt
9   1999.0      1    1     1       30       73.0    6.9  mlrf1c1999.txt
10  1999.0      1    1     1       40       78.0    6.5  mlrf1c1999.txt
11  1999.0      1    1     1       50       75.0    7.3  mlrf1c1999.txt
1147812  1997.0     12   31    21        0      261.0    6.0  mlrf1c1997.txt
1147813  1997.0     12   31    21       10      260.0    5.9  mlrf1c1997.txt
1147814  1997.0     12   31    21       20      262.0    5.5  mlrf1c1997.txt
1147815  1997.0     12   31    21       30      279.0    6.5  mlrf1c1997.txt
1147816  1997.0     12   31    21       40      283.0    7.3  mlrf1c1997.txt
1147817  1997.0     12   31    21       50      282.0    7.2  mlrf1c1997.txt
1147818  1997.0     12   31    22        0      277.0    6.9  mlrf1c1997.txt
1147819  1997.0     12   31    22       10      283.0    7.6  mlrf1c1997.txt
1147820  1997.0     12   31    22       20      283.0    7.2  mlrf1c1997.txt
1147821  1997.0     12   31    22       30      290.0    7.5  mlrf1c1997.txt
1147822  1997.0     12   31    22       40      289.0    7.2  mlrf1c1997.txt
1147823  1997.0     12   31    22       50      292.0    7.6  mlrf1c1997.txt
1147824  1997.0     12   31    23        0      296.0    7.7  mlrf1c1997.txt"""    

df = pd.read_csv(io.StringIO(doc), sep='\s+')

脾氣暴躁的筆記:在一個更好的問題中,上面的代碼是有問題的,需要一些不必要的練習和時間來復制它。 有關詳細信息,請參閱https://stackoverflow.com/help/mcve

# Now you need a function accepting an argument for `aggfunc`

def avg(x):
    # x will be a pd.Series, equalling df.direction
    return circmean(x, high=x.max(), low=x.min())
    
# just to learn how it works with 'mean'
df2 = df.pivot_table(values='direction', index=['day', 'hour'], aggfunc = 'mean')

# now putting the desired function
df3 = df.pivot_table(values='direction', index=['day', 'hour'], aggfunc = avg)

有一個警告,但我希望你知道處理它(也許你想在avg中將度數轉換為弧度):

運行時警告:在 true_divide 中遇到無效值 ang = (samples - low) 2 pi / (high - low)

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM