当与另一列的平均值相比时,熊猫返回1或0

时间:2017-02-03 19:16:00

标签: python python-3.x pandas

我有一个数据帧,我正在尝试创建一个返回1或0的新列。逻辑是,如果状态的['Avg_Temp']大于或等于['Avg_Temp']。 ()然后我希望['Hot']列的值为'1',否则它的值应为'0'。

我的尝试在下面,但我对df ['Hot']的结果都是'1',所以我知道我做错了...

提前感谢您的帮助!

数据=

    Avg_Temp
MI  66          
AL  78
OH  50
VT  64
NB  34

df = data
TempMean = df10['Avg_Temp'].mean()
df10['Hot'] = 0
df10['Hot'] = df10.loc[df10['Avg_Temp'] >= TempMean] = 1
return df10['Hot']

4 个答案:

答案 0 :(得分:2)

df10['Hot'] = df10.loc[df10['Avg_Temp'] >= TempMean] = 1

不符合您的意图。这相当于

df10.loc[df10['Avg_Temp'] >= TempMean] = 1
df10['Hot'] = 1

首先将Avg_Temp列> = TempMean的每一行设置为1,然后将Hot列设置为1.

你可能想要

In [51]: df10["Hot"] = 0

In [52]: df10.loc[df10["Avg_Temp"] >= TempMean, "Hot"] = 1

In [53]: df10
Out[53]: 
    Avg_Temp  Hot
MI        66    1
AL        78    1
OH        50    0
VT        64    1
NB        34    0

或更简单,

df10["Hot"] = (df10["Avg_Temp"] >= TempMean).astype(int)

我们不必先设置df10["Hot"]

答案 1 :(得分:1)

错误在于这一行:

df10['Hot'] = df10.loc[df10['Avg_Temp'] >= TempMean] = 1

df10.loc[df10['Avg_Temp'] >= TempMean] = 1的含义是:"将1设置为符合条件"的行。然后,将此操作的结果分配给列Hot。你想要做的是用以下代码替换该行:

df10.loc[df10['Avg_Temp'] >= TempMean, 'Hot'] = 1

答案 2 :(得分:1)

我喜欢

avg = df.Avg_Temp
df.assign(Hot=avg.ge(avg.mean()).astype(np.uint8))

答案 3 :(得分:0)

尝试

df['Hot'] = np.where((df['Avg_Temp'] > df['Avg_Temp'].mean()), 1, 0)