小编fly*_*our的帖子

如何从数据帧组的第一个值创建增量var?

我有一个datframe为:

data=[[0,1,5],
      [0,1,6],
      [0,0,8],
      [0,0,10],
      [0,1,12],
      [0,0,14],
      [0,1,16],
      [0,1,18],    
      [1,0,2],
      [1,1,0],
      [1,0,1],
      [1,0,2]]

df = pd.DataFrame(data,columns=['KEY','COND','VAL'])
Run Code Online (Sandbox Code Playgroud)

对于RES1,我想创建一个计数器变量RES,其中COND == 1。组中第一个KEY的RES值与VAL相同(我可以以某种方式使用cumcount())。

对于RES2,我只想将缺少的值填充为先前的值。(df.fillna(method='ffill')), 我在想..

    KEY  COND    VAL RES1  RES2
0     0     1    5      5     5
1     0     1    6      6     6
2     0     0    8            6
3     0     0   10            6
4     0     1   12      7     7
5     0     0   14            7
6     0     1   16      8     8
7     0     1   18      9     9
8     1     0    2      2     2
9 …
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

5
推荐指数
1
解决办法
49
查看次数

不可对齐的布尔系列作为索引器提供

我正在尝试将数据框过滤为:

a= a[~(b['var1'].isin(c['var2']))]
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

"Unalignable boolean Series provided as "
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
Run Code Online (Sandbox Code Playgroud)

我理解该声明:

print(~(b['var1'].isin(c['var2'])) [:10])
Run Code Online (Sandbox Code Playgroud)

返回一个布尔掩码的系列,这可能是不可接受的。所以,我尝试将 loc 用作:

a= a.loc[:, ~(b['var1'].isin(c['var2']))]
Run Code Online (Sandbox Code Playgroud)

但我遇到了同样的错误。我在这里缺少什么?任何意见将不胜感激。

谢谢

python filtering pandas

4
推荐指数
1
解决办法
1万
查看次数

How do I make NaN numeric values to be lower than any other numeric value.during comparisons?

Bottomline, I want In a comparison, NaN numeric values to be lower than any other numeric value.

Lets say I have s1 and s2,

s1 = pd.Series([1, 3, np.nan, 5, np.nan, -1, np.nan])
s2 = pd.Series([2, 1, np.nan, np.nan, 2, np.nan, -1])
Run Code Online (Sandbox Code Playgroud)

When I compare them as s1 < s2 then I want the following behavior:

Out: 
0    True
1    False
2    False
3    False
4    True
5    False
6    True
Run Code Online (Sandbox Code Playgroud)

python nan pandas

3
推荐指数
1
解决办法
68
查看次数

标签 统计

pandas ×3

python ×3

filtering ×1

nan ×1

numpy ×1