我必须每月规范化一个dataframe列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,除以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
...我的方法是使用迭代:
for row in data.itertuples(index=True,name='index'):
if row.index =='2018-11':
data['Allocation']/Volume[0]在这里,如果声明是不正确的..。
另一种方法是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'
我可以用这种方法解决我的问题,还是应该使用不同的方法?任何帮助我都很高兴
干杯!
发布于 2019-06-25 12:22:42
也许您可以将列表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"]https://stackoverflow.com/questions/56753511
复制相似问题