如何计算python中数据框中的行?

时间:2015-02-05 15:37:41

标签: python numpy pandas

import numpy as np
import pandas as pd 

year=np.array([1851.203,1851.632,1851.969,1851.975,1852.314,1852.347,1852.358,1852.385,1852.977,1853.196,1853.229,1853.319,1853.5,1854.135,1856.396,1856.506,1856.539,1856.618,1857.138,1857.404])
coal=pd.DataFrame(year)

def disaster(year1,year2):
    disaster = (coal >= year1) & (coal <= year2)
    print year.sum()

disaster(1852,1856)
>>>360806.155

我想写一个函数,它会给我两年之间的年数。就像我把年份1852,1856放在一起的所有年份,但我想要在这个间隔之间有多少行(整数)。

例如,1851年是4次,1852年是5次,当我把时间间隔(1851,1852)时,它将总结并给出9作为我想要的任何两年。

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我已根据一些假设修改了您的代码。我认为你要做的是使用你的面具掩盖到原始的df,打印或得到总和,并另外得到长度。要获得使用原始df的掩码所需的长度,请调用dropna()以删除NaN行,然后在生成的df上调用len

In [16]:

year=np.array([1851.203,1851.632,1851.969,1851.975,1852.314,1852.347,1852.358,1852.385,1852.977,1853.196,1853.229,1853.319,1853.5,1854.135,1856.396,1856.506,1856.539,1856.618,1857.138,1857.404])
coal=pd.DataFrame(year)

def disaster(year1,year2):
    total = (coal >= year1) & (coal <= year2)
    print (coal[total].sum())
    print(len(coal[total].dropna()))

disaster(1852,1856)
0    18529.76
dtype: float64
10

答案 1 :(得分:0)

def disaster(year1,year2):
    return  np.sum((coal >= year1) & (coal <= year2+1))

print disaster(1851,1852)

>>> 
0    9
dtype: int64