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

Python:如何处理DataFrame中列表列内以'A'开头的字符串截断

Solution for Processing List Entries in DataFrame

Got it, let's walk through how to handle this task. We'll use pandas to process each string in your Entities column lists, modifying only those that start with 'A' by stripping everything after the '/'.

Step 1: Define the processing function

First, create a helper function that takes a single string and applies your rules:

def process_entity(s):
    # Check if the string starts with 'A' (case-sensitive)
    if s.startswith('A'):
        # Split on '/' and keep only the part before it
        return s.split('/')[0]
    # Leave other strings unchanged
    return s

Step 2: Apply the function to your DataFrame

Next, use pandas' apply method to run this function on every item in each list in the Entities column:

import pandas as pd

# Your input DataFrame
df = pd.DataFrame({
    'Entities': [
        ["Apple/1", "Applet/87", "Book/12", "Stable/0"],
        ["App/12", "orange/6", "Apples/7", "Stables/0"]
    ]
})

# Process each list in the Entities column
df['Entities'] = df['Entities'].apply(lambda lst: [process_entity(item) for item in lst])

Step 3: Verify the output

After running the code, your DataFrame will match your desired result exactly:

Entities
0  [Apple, Applet, Book/12, Stable/0]
1  [App, orange/6, Apples, Stables/0]

This approach is efficient because it operates row-wise and only modifies the strings that meet your criteria, leaving all other entries untouched.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:39:43