我正在根据条件在一个独立的df列中使用1或0来“平放”一些数据,但是可以使用一些技巧.
编辑,此问题不是查找数据格式中的数据,而是试图根据行条件为每一行寻找修改数据文件中的值的解决方案。
由以下数据组成:
import pandas as pd
import numpy as np
rows,cols = 8760,3
data = np.random.rand(rows,cols)
tidx = pd.date_range('2019-01-01', periods=rows, freq='1T')
df = pd.DataFrame(data, columns=['cooling_sig','heating_sig','economizer_sig'], index=tidx)这是我的应用程序的一些额外参数和列:
# params for air handling unit (ahu)
ahu_min_oa = .2
# make columns out of params
df['ahu_min_oa'] = ahu_min_oa
df['heating_mode'] = 0
df['econ_mode'] = 0
df['econ+mech_cooling'] = 0
df['mech_cooling'] = 0一个处理数据的函数,但它不起作用。任何更好的实践都会受到极大的赞赏,除了锤击数据帧的每一行之外。我正在尝试“标记”一种基于条件的值为1的模式。例如,对于数据中的每一行,如果heating_mode大于零,则heating_sig将为True或1。
def data_preprocess(dataframe):
for index, row in dataframe.iterrows():
# OS1, the AHU is heating
if row.heating_sig > 0:
row['heating_mode'] = 1
# OS2, the AHU is using free cooling only
if row.economizer_sig > row.ahu_min_oa and row.cooling_sig == 0:
row['econ_mode'] = 1
# OS3, the AHU is using free and mechanical cooling
if row.economizer_sig > row.ahu_min_oa and row.cooling_sig > 0:
row['econ+mech_cooling'] = 1
# OS4, the AHU is using mechanical cooling only
if row.economizer_sig <= row.ahu_min_oa and row.cooling_sig > 0:
row['mech_cooling'] = 1
return dataframe对不起,可能是一个奇怪的申请和问题,但感谢您的任何提示。我试图标记一些数据不起作用,所有的value_counts()都是零。
df['heating_mode'].value_counts()
df['mech_cooling'].value_counts()
df['econ_mode'].value_counts()
df['econ+mech_cooling'].value_counts()发布于 2021-11-15 15:21:32
您不需要(也不应该)迭代您的DataFrame。
相反,试着:
df.loc[df["heating_sig"].eq(1), "heating_mode"] = 1
df.loc[df["economizer_sig"].gt(df["ahu_min_oa"]) & df["cooling_sig"].eq(0), "econ_mode"] = 1
df.loc[df["economizer_sig"].gt(df["ahu_min_oa"]) & df["cooling_sig"].gt(0), "econ+mech_cooling"] = 1
df.loc[df["economizer_sig"].le(df["ahu_min_oa"]) & df["cooling_sig"].gt(0), "mech_cooling"] = 1发布于 2021-11-15 15:30:30
可能有更有效的方法来进行同样的操作,但是如果您确实需要使用iterrow(),那么请遵循以下方法:
def data_preprocess(dataframe):
for index, row in dataframe.iterrows():
# OS1, the AHU is heating
if row.heating_sig > 0:
dataframe.at[index, 'heating_mode'] = 1
# OS2, the AHU is using free cooling only
if row.economizer_sig > row.ahu_min_oa and row.cooling_sig == 0:
dataframe.at[index, 'econ_mode'] = 1
# OS3, the AHU is using free and mechanical cooling
if row.economizer_sig > row.ahu_min_oa and row.cooling_sig > 0:
dataframe.at[index, 'econ+mech_cooling'] = 1
# OS4, the AHU is using mechanical cooling only
if row.economizer_sig <= row.ahu_min_oa and row.cooling_sig > 0:
dataframe.at[index, 'mech_cooling'] = 1
return dataframehttps://stackoverflow.com/questions/69976654
复制相似问题