You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python技术问询:获取Pandas Series符合条件元素的位置、构建关联DataFrame的pos列及索引位置数组

Solution to Your Pandas/Numpy Questions

Let's break down each of your questions with clear, actionable code examples:

1. Calculating the 'pos' column for all_edges DataFrame

The 'pos' column needs to map each edge entry to its row position in either up_edge or down_edge (based on whether it's an upward edge 1 or downward edge -1). Here's how to do it:

First, create mapping dictionaries that link each index value in up_edge and down_edge to their respective row positions:

# Create position mappings for up_edge and down_edge
up_pos_map = dict(zip(up_edge.index, range(len(up_edge))))
down_pos_map = dict(zip(down_edge.index, range(len(down_edge))))

Then, populate the 'pos' column in all_edges by checking the 'edge' value and using the corresponding mapping:

import numpy as np

# Build the all_edges DataFrame with the pos column
all_edges = pd.DataFrame({
    'y': all_edges_y,
    'edge': edge_or_not[edge_or_not != 0].to_numpy(),
    'pos': np.where(
        all_edges['edge'] == 1,
        all_edges.index.map(up_pos_map),
        all_edges.index.map(down_pos_map)
    )
}, index=all_edges_x)

This will produce exactly the all_edges structure you need, where each entry's 'pos' matches its row index in either up_edge or down_edge. You can then use this column to cross-reference between DataFrames as shown in your example.

2. Getting the array of all index positions for a DataFrame

To get an array of integer positions (like [0, 1, 2, 3] for down_edge), you have a few simple options:

  • Use range() wrapped in a list:
    down_edge_positions = list(range(len(down_edge)))
    # Output: [0, 1, 2, 3]
    
  • Use numpy.arange() for a numpy array:
    import numpy as np
    down_edge_positions = np.arange(len(down_edge))
    # Output: array([0, 1, 2, 3])
    
  • If you need to map index labels to their positions, you can use get_indexer():
    down_edge_positions = down_edge.index.get_indexer(down_edge.index)
    # Output: array([0, 1, 2, 3])
    

3. Getting positions of elements in a Pandas Series that meet a condition

If you're referring to label indices (the index values of the Series), use:

# Get label indices where edge_or_not is not 0
condition_indices = edge_or_not[edge_or_not != 0].index

If you need integer positions (the positional index in the Series, not the label), use np.where():

import numpy as np
# Get integer positions where edge_or_not is not 0
condition_positions = np.where(edge_or_not != 0)[0]
# Output: array([1, 3, 6, 7, 8, 9])

Alternatively, you can use Series.index.get_indexer() to map the filtered label indices to their integer positions:

condition_positions = edge_or_not.index.get_indexer(edge_or_not[edge_or_not != 0].index)

内容的提问来源于stack exchange,提问作者Ren Ningen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:07:42