python - Find value and expand it to the grouping in pandas
问题描述
I need to transform pandas toy-dataframe like this (basically group by entity
, find the value of v
for which df['gr'] == 'x'
and "expand" that value to the entire grouping):
entity gr v
0 A x 1
1 A y 2
2 A z 3
3 B z 4
4 B x 5
5 B y 6
to this form:
entity gr v new
0 A x 1 1
1 A y 2 1
2 A z 3 1
3 B z 4 5
4 B x 5 5
5 B y 6 5
Here is my solution:
import pandas as pd
df = pd.DataFrame({'entity': ['A', 'A', 'A','B', 'B', 'B'], 'gr': ['x', 'y', 'z', 'z', 'x', 'y'], 'v': [1,2,3,4,5,6]})
df['new'] = df.loc[df['gr'] == 'x', 'v']
df['new'] = df.groupby('entity')['new'].ffill().bfill().astype(int)
but I wonder, if a better, more concise or idiomatic approach exists to this problem?
Slight variation on this problem, instead of df['gr'] == 'x'
, different mask df['gr'] == df['different_column']
解决方案
If always match only one value or no value per entity
is possible filter first, then convert entity
to index and use Series.map
:
df['new'] = df['entity'].map(df[df['gr'] == 'x'].set_index('entity')['v'])
print (df)
entity gr v new
0 A x 1 1
1 A y 2 1
2 A z 3 1
3 B z 4 5
4 B x 5 5
5 B y 6 5
Your solution should be changed by GroupBy.first
in GroupBy.transform
:
df['new'] = (df.assign(new = df['v'].where(df['gr'] == 'x'))
.groupby('entity')['new'].transform('first'))
print (df)
entity gr v new
0 A x 1 1.0
1 A y 2 1.0
2 A z 3 1.0
3 B z 4 5.0
4 B x 5 5.0
5 B y 6 5.0
推荐阅读
- php - laravel 中的这个 auth 层如何不创建循环?
- javascript - 可以将 Promise 值发送到 next() 内部的函数而不是 next() 内部的回调吗?
- python - 如何获取每个已识别集群的数据点计数?
- sql - 对字符串中的十进制数求和
- python - 是否可以使用 pyautogui 和 win32gui/pywin32 等模块通过 Web 应用程序访问 PC 系统?
- sql-server - SSIS Excel 到 SQL Server - 列故障的长数据
- linux - 如何让 dash shell 运行一些用于 sh (bash) 的命令?
- c++ - 如何在 VC++ MFC 应用程序中使用 UTF-8 编码
- go - bleve中的模糊搜索
- angular - 在嵌套表单元素上使用角度材料表单字段错误消息