繁体   English   中英

python如何计算if语句中的虚拟变量

[英]python how to count dummy variable in if statement

我有一个数据框“总计”,包括如下列:

  • 纬度(纬度)
  • 经度(lon)
  • f2(ex ) 1950-12-06, 1959-08-01,...) 代表某一天
  • F_chicken(1:chicken, 0: not chicken, dummy variable)

我想计算 F_chicken 的数量,其中一些行 'lat' 和 'lon' 相同,而一些行 'f2' 较小。

我的 dataframe.head()]

我尝试使用 for 循环制作这个c_chicken列,但失败了...

n = len(total['f2'])
def col_counts(col):
    count = []
    for i,j in range(n):
        if (i != j) and (total['f2'][i] <= total['f2'][j]) and (total['lat'][i]==total['lat'][j]) and (total['lon'][i]==total['lon'][j]) and(col[j] == 1): count[i] += 1
    return count
total['c_chicken'] = col_counts(total.F_chicken)

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)
<ipython-input-114-879544b4f09a> in <module>
----> 1 total['c_chicken'] = col_counts(total.F_chicken)

<ipython-input-113-ece8cb8d9ef5> in col_counts(col)
      2 def col_counts(col):
      3     count = []
----> 4     for i,j in range(n):
      5         if (i != j) and (total['f2'][i] <= total['f2'][j]) and 
(total['lat'][i]==total['lat'][j]) and (total['lon'][i]==total['lon'][j]) 
and(col[j] == 1): count[i] += 1
      6     return count

TypeError: cannot unpack non-iterable int object

完全无视你的逻辑。

错误在您的循环语句中,range(n) 返回一个迭代器,您正在尝试将其解压缩为 2,即ij

有什么理由不能使用嵌套的 for 循环?

for i in range(n):
   for j in range(n):
       #code that uses i and j

现在为你的逻辑

我建议使用 Pandas 方法而不是显式 for 循环。
如果f2是日期时间格式,那很好。 否则,您应该通过以下方式将其转换为日期时间格式

total['f2'] = pd.to_datetime(total['f2'], format='%Y-%m-%d')

由于您想要具有最小f2值的行,您应该对f2列上的数据框进行排序。

total.sort_values(by='f2')

现在,您可以使用keep = first删除基于latlon重复项,并计算F_chicken == 1行数

tmp = total.drop_duplicates(['lat', 'lon'], keep='first')
total['c_chicken'] = tmp[tmp['F_chicken'] == 1].shape[0] # assuming it is int not str, otherwise use '1'

我假设您知道它将为整个c_chickens列分配相同的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM