簡體   English   中英

根據索引值對數據框列執行計算

[英]Perform calculations on dataframe column based on index value

我必須每月標准化一個數據框列Allocation值。

data=
                     Allocation  Temperature  Precipitation  Radiation
Date_From                                                             
2018-11-01 00:00:00    0.001905         9.55            0.0        0.0
2018-11-01 00:15:00    0.001794         9.55            0.0        0.0
2018-11-01 00:30:00    0.001700         9.55            0.0        0.0
2018-11-01 00:45:00    0.001607         9.55            0.0        0.0

這意味着,如果我們有 2018-11,將Allocation除以 11.116,而在 2018-12 中,將Allocation除以 2473.65,依此類推......(這些值來自列表Volume ,其中Volume[0]對應於 2018- 11 直到Volume[7]對應於 2019-06)。

Date_From是一個索引和一個時間戳。

data_normalized=
                     Allocation  Temperature  Precipitation  Radiation
Date_From                                                             
2018-11-01 00:00:00    0.000171         9.55            0.0        0.0
2018-11-01 00:15:00    0.000097         9.55            0.0        0.0
...

我的方法是使用 itertuples:

for row in data.itertuples(index=True,name='index'):
    if row.index =='2018-11':
        data['Allocation']/Volume[0]

在這里,if 語句永遠不會正確......

另一種方法是if ((row.index >='2018-11-01 00:00:00') & (row.index<='2018-11-31 23:45:00')):但是,這里我得到錯誤TypeError: '>=' not supported between instances of 'builtin_function_or_method' and 'str'

我可以用這種方法解決我的問題還是應該使用不同的方法? 我很高興任何幫助

干杯!

也許您可以將您的列表Volume放在一個數據框中,其中日期(或索引)是每個月的第一天。

import pandas as pd
import numpy as np

N = 16
date = pd.date_range(start='2018-01-01', periods=N, freq="15d")
df = pd.DataFrame({"date":date, "Allocation":np.random.randn(N)})

# A dataframe where at every month associate a volume
df_vol = pd.DataFrame({"month":pd.date_range(start="2018-01-01", periods=8, freq="MS"),
                       "Volume": np.arange(8)+1})

# convert every date with the beginning of the month
df["month"] = df["date"].astype("datetime64[M]")

# merge
df1 = pd.merge(df,df_vol, on="month", how="left")

# divide allocation by Volume. 
# Now it's vectorial as to every date we merged the right volume.
df1["norm"] = df1["Allocation"]/df1["Volume"]

暫無
暫無

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

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