简体   繁体   English

如何及时找到所有那些在对应的 X 元素之前的 Y 元素? - Pandas, Python

[英]How to find all those Y elements that precede the corresponding X elements in time? - Pandas, Python

I am using Pandas to try to find all those Y elements that precede the corresponding X elements in time.我正在使用 Pandas 尝试及时找到在相应 X 元素之前的所有 Y 元素。

df = {'time':[1,2,3,4,5,6,7,8], 'X':['x','w','r','a','k','y','u','xa'],'Y':['r','xa','a','x','w','u','k','y']}

df = pd.DataFrame.from_dict(df)

time    X   Y
0   1   x   r
1   2   w   xa
2   3   r   a
3   4   a   x
4   5   k   w
5   6   y   u
6   7   u   k
7   8   xa  y

What I would like to achieve is:我想要实现的是:

time    X   Y
0   1   x   r
1   2   w   xa
2   3   r   a
5   6   y   u

Any ideas?有任何想法吗?

You can make two dictionaries which keep track of the indexes.您可以制作两个跟踪索引的字典。 Then use pd.Series.map to get boolean index then use boolean indexing然后使用pd.Series.map得到 boolean 索引然后使用boolean indexing

idx = dict(zip(df['X'],df['time']))
idx2 = dict(zip(df['Y'],df['time']))
mask = df['Y'].map(lambda k: idx[k]>idx2[k]
df[mask]
   time  X   Y
0     1  x   r
1     2  w  xa
2     3  r   a
5     6  y   u

df.apply over axis 1 is not recommended it should be as your last resort. df.apply over axis 1 不推荐它应该作为你最后的手段。 Check out why看看为什么

Here's timeit analysis which supports the statement.这是支持该声明的 timeit 分析。

In [74]: %%timeit
    ...: df[df.apply(lambda row: row['Y'] in df.loc[row.time:,'X'].values, axis=1)]
    ...:
    ...:
2.26 ms ± 203 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [80]: %%timeit
    ...: idx = dict(zip(df['X'],df['time']))
    ...: idx2 = dict(zip(df['Y'],df['time']))
    ...: mask = df['Y'].map(lambda k: idx[k]>idx2[k])
    ...: x = df[mask]
    ...:
    ...:
498 µs ± 30.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Almost 5X faster.几乎快 5 倍。

Try this:试试这个:

result = df[df.apply(lambda row: row['Y'] in df.loc[row.time:,'X'].values, axis=1)]

print(result)

   time  X   Y
0     1  x   r
1     2  w  xa
2     3  r   a
5     6  y   u

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Pandas数据帧中,如何用元素y替换所有元素x? - In a Pandas dataframe, how to replace all elements x with element y? Python Pandas:如何读取列表中的所有元素并从 dataframe 中检索相应的值 - Python Pandas: how to read all elements in a list and retrieve the corresponding values from dataframe 如何将一行中的所有元素与 pandas dataframe 中的 Python 中的同一行中的相应元素相乘? - How to multiply all elements in a row with corresponding element in same row in different column in pandas dataframe in Python? 如何使用元素 [x,y] 获取大小为 n 的所有可能向量 - How to get all possible vectors of size n with elements [x,y] 在Selenium python中查找具有相应信息的元素 - Find Elements with corresponding information in Selenium python python-访问所有与key不对应的字典元素 - python - access all dictionary elements not corresponding to key 如何使用 pandas 查找对应的编码元素 - How to lookup corresponding encoded elements with pandas 如何使用 Numpy 将矩阵 x 的所有元素乘以矩阵 y 的所有元素? - How to use Numpy to multiply all elements of matrix x by all elements of matrix y? 如何在Python中重复y次重复x个元素数量 - How to increment x number of elements repeatedly y times in Python 找出两个列表 x 和 y 之间的所有配对组合,使得 y 中的所有元素都与 x 中的一个完全配对 - Find all combinations of pairings between two lists x and y, such that all elements from y are paired with exactly one from x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM