对列中的所有行对执行操作 [英] Perform an operation on all pairs of rows in a column
问题描述
假设以下DataFrame:
Assume the following DataFrame:
id A
1 0
2 10
3 200
4 3000
我想在所有行到所有其他行之间进行计算.
例如,如果计算为lambda r1, r2: abs(r1-r2)
,则输出将为(按某种顺序)
I would like to make a calculation betweeen all rows to all other rows.
For example, if the calculation were lambda r1, r2: abs(r1-r2)
, then the output would be (in some order)
id col_name
1 10
2 200
3 3000
4 190
5 2990
6 2800
问题:
- 如何仅获得上述输出?
- 如何以最像熊猫一样"的方式将结果与创作者联系起来?
我想将所有内容尽可能地保留在单个表中,以仍然支持合理查找的方式.
I would like to keep everything in a single table as much as possible, in a way that still supports reasonable lookup.
我的数据量并不大,而且永远也不会.
The size of my data is not large, and never will be.
回答我的问题2的一种方法是
One way that would answer my question 2 would be
id col_name origin1 origin2
1 10 1 2
2 200 1 3
3 3000 1 4
4 190 2 3
5 2990 2 4
6 2800 3 4
我想知道这是否是标准的,是否有内置的方法,或者是否还有其他/更好的方法
And I would like to know if this is standard, and has a built in way of doing this, or if there is another/better way
推荐答案
IIUC itertools
import itertools
s=list(itertools.combinations(df.index, 2))
pd.Series([df.A.loc[x[1]]-df.A.loc[x[0]] for x in s ])
Out[495]:
0 10
1 200
2 3000
3 190
4 2990
5 2800
dtype: int64
更新
s=list(itertools.combinations(df.index, 2))
pd.DataFrame([x+(df.A.loc[x[1]]-df.A.loc[x[0]],) for x in s ])
Out[518]:
0 1 2
0 0 1 10
1 0 2 200
2 0 3 3000
3 1 2 190
4 1 3 2990
5 2 3 2800
这篇关于对列中的所有行对执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!