python - 面板数据: create new variable with a condition

标签 python pandas

我正在尝试从以下df创建一个新变量。我想创建一个新的二进制(0 或 1)变量,如果在任何一个季度变量 entr 等于 1,并且按 id,则该变量在一年中的每个季度放置 1。

此数据框的示例转换为:

id     year    quarter   fecha      entr
 1    2007          1     220       0      
 1    2007          2     221       0    
 1    2007          3     222       0 
 1    2007          4     223       0      
 1    2008          1     224       0
 1    2008          2     225       0
 1    2008          3     226       1
 1    2008          4     227       0
 1    2009          1     228       0
 1    2009          2     229       0
 1    2009          3     230       0
 1    2009          4     231       0
 2    2007          1     220       0      
 2    2007          2     221       0    
 2    2007          3     222       0 
 2    2007          4     223       0      
 2    2008          1     224       0
 2    2008          2     225       0
 2    2008          3     226       0
 2    2008          4     227       0
 2    2009          1     228       0
 2    2009          2     229       1
 2    2009          3     230       0
 2    2009          4     231       0     

结果应该是这样的:

id     year    quarter   fecha      entr   new variable
 1    2007          1     220       0           0
 1    2007          2     221       0           0
 1    2007          3     222       0           0
 1    2007          4     223       0           0
 1    2008          1     224       0           1
 1    2008          2     225       0           1
 1    2008          3     226       1           1
 1    2008          4     227       0           1
 1    2009          1     228       0           0
 1    2009          2     229       0           0
 1    2009          3     230       0           0
 1    2009          4     231       0           0
 2    2007          1     220       0           0
 2    2007          2     221       0           0
 2    2007          3     222       0           0
 2    2007          4     223       0           0
 2    2008          1     224       0           0
 2    2008          2     225       0           0
 2    2008          3     226       0           0
 2    2008          4     227       0           0
 2    2009          1     228       0           1
 2    2009          2     229       1           1
 2    2009          3     230       0           1
 2    2009          4     231       0           1

非常感谢你,对我的英语感到抱歉。

最佳答案

idyear上调用groupby,在entr<上调用transform('max')/:

v = df.groupby(['id', 'year']).entr.transform('max')

v
0     0
1     0
2     0
3     0
4     1
5     1
6     1
7     1
8     0
9     0
10    0
11    0
12    0
13    0
14    0
15    0
16    0
17    0
18    0
19    0
20    1
21    1
22    1
23    1
Name: entr, dtype: int64
df['new variable'] = v
<小时/>

如果最大值可能大于 1,您可以对结果进行 groupby 并调用 clip:

df.groupby(['id', 'year']).entr.transform('max').clip(0, 1)

0     0
1     0
2     0
3     0
4     1
5     1
6     1
7     1
8     0
9     0
10    0
11    0
12    0
13    0
14    0
15    0
16    0
17    0
18    0
19    0
20    1
21    1
22    1
23    1
Name: entr, dtype: int64

这也应该适用于 Vaishali 的答案。

关于python - 面板数据: create new variable with a condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47400775/

相关文章:

javascript - 使用 ajax 从 javascript 生成 python 代码

python - 尝试使用 Python request.put 更新 Openhab 的 Rest API 中的值时出现错误 404

python - pandas 过滤不包含任何内容的日期时间列

python - 多索引中的标签输出

python-3.x - 如何在 Pandas 数据帧上迭代 TfidfVectorizer()

pandas - pyspark 的 pandas 中的 flatMap

python - Numpy 广播

python - 如何在两个子图上添加小记号?

python - Pandas 数据框 : copy the contents of a column if it is empty

python - Pandas Python : sort dataframe but don't include given row