一个相当直截了当的熊猫问题:
如果我有这样的数据帧:
hour
0 0
1 1
2 1
3 2
4 2
...
我想创建一个新的列'lunch',如果11
最佳答案
你可以
In [231]: df['lunch'] = (df['hour']<=11) & (df['hour']<=1)
In [232]: df['lunch']
Out[232]:
0 True
1 True
2 True
3 False
4 False
Name: lunch, dtype: bool
In [233]: df['lunch'].astype(int)
Out[233]:
0 1
1 1
2 1
3 0
4 0
Name: lunch, dtype: int32