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

Pandas DataFrame字符串修改与坐标点采样密度调整问题咨询

实现坐标点采样减半的Pandas处理方案

你之前的初步处理思路存在问题:直接把|替换为;再整体拆分,会丢失坐标对的分组关系,导致x、y的对应关系混乱,建议按以下逻辑实现:


完整实现代码

import pandas as pd

# 如果你已经执行过之前的错误初步处理,先删掉生成的无效数字列
df = df.drop(columns=[col for col in df.columns if isinstance(col, int) or col.isdigit()])

# 定义单行处理函数
def process_single_row(row):
    # 按|拆分得到所有原始坐标对,长度等于points值
    all_point_pairs = row['values'].split('|')
    # 步长为2切片采样,天然保留首尾,采样数为原来的1/2
    sampled_pairs = all_point_pairs[::2]
    # 拆分坐标对为x、y的一维列表
    expand_value = []
    for pair in sampled_pairs:
        x_val, y_val = pair.split(';')
        expand_value.extend([x_val, y_val])
    # 生成对应列名
    col_names = []
    for idx in range(len(sampled_pairs)):
        col_names.append(f'x{idx+1}')
        col_names.append(f'y{idx+1}')
    return pd.Series(expand_value, index=col_names)

# 合并处理结果到原表
df_result = pd.concat([df, df.apply(process_single_row, axis=1)], axis=1)

效果验证

以你给出的ID=1的行为例:

  • 原始拆分后坐标对共8个,采样后得到4个:0,5;1、4;6、8;10、15;18
  • 展开后得到x1=0,5、y1=1、x2=4、y2=6、x3=8、y3=10、x4=15、y4=18,和你期望的输出完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 12:36:01