hli*_*117 7 python search pandas
假设我有以下数据帧:
'a' 'b'
0 0 0
1 1 0
2 0 1
3 0 1
Run Code Online (Sandbox Code Playgroud)
有没有办法可以获得特定值存在的索引/列值?例如,类似于以下内容:
values = df.search(1)
Run Code Online (Sandbox Code Playgroud)
会的values = [(1, 'a'), (2, 'b'), (3, 'b')].
Ale*_*lex 11
df[df == 1].stack().index.tolist()
Run Code Online (Sandbox Code Playgroud)
产量
[(1, 'a'), (2, 'b'), (3, 'b')]
Run Code Online (Sandbox Code Playgroud)
如果您不介意使用 NumPy 数组,其中第一列表示索引位置,第二列表示列名的索引,因为它驻留在 中df.columns,那么它非常短:
In [11]: np.argwhere(df)
Out[11]:
array([[1, 0],
[2, 1],
[3, 1]])
Run Code Online (Sandbox Code Playgroud)
如果要将其格式化为具有实际列名的元组列表,您可以进一步执行以下操作:
In [12]: [(x, df.columns[y]) for x,y in np.argwhere(df)]
Out[12]: [(1, 'a'), (2, 'b'), (3, 'b')]
Run Code Online (Sandbox Code Playgroud)
您可以对 内部的逻辑表达式使用相同的方法,np.argwhere例如,假设您有一些随机数据的 DataFrame:
In [13]: dfrm
Out[13]:
A B C
0 0.382531 0.287066 0.345749
1 0.725201 0.450656 0.336720
2 0.146883 0.266518 0.011339
3 0.111154 0.190367 0.275750
4 0.757144 0.283361 0.736129
5 0.039405 0.643290 0.383777
6 0.632230 0.434664 0.094089
7 0.658512 0.368150 0.433340
8 0.062180 0.523572 0.505400
9 0.287539 0.899436 0.194938
[10 rows x 3 columns]
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做,例如:
In [14]: [(x, dfrm.columns[y]) for x,y in np.argwhere(dfrm > 0.8)]
Out[14]: [(9, 'B')]
Run Code Online (Sandbox Code Playgroud)
作为一个搜索函数,它可以这样定义:
def search(df, df_condition):
return [(x, df.columns[y]) for x,y in np.argwhere(df_condition(df))]
Run Code Online (Sandbox Code Playgroud)
例如:
In [17]: search(dfrm, lambda x: x > 0.8)
Out[17]: [(9, 'B')]
In [18]: search(df, lambda x: x == 1)
Out[18]: [(1, 'a'), (2, 'b'), (3, 'b')]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5692 次 |
| 最近记录: |