Python:如何快速处理列中的值

2024-04-20 15:48:46 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,有一个数据帧类似于下面的数据帧df1。数据类型为string。你知道吗

    eye         nose       mouse       ear
  34_35_a      45_66_b    45_64_a     78_87_a
  35_38_a      75_76_b    95_37_a     38_79_a
  64_43_a      85_66_b    65_45_a     87_45_a

我想得到如下数据帧那样的数据帧。眼数据分为眼x、眼y,其他列相同,数据类型为float。你知道吗

 eye_x   eye_y    nose_x   nose_y     mouse_x  mouse_y     ear_x   ear_y        
    34       35       45       66         45        64        78       87
    35       38       75       76         95        37        38       79
    64       43       85       66         65        45        87       45

到目前为止,我知道如何通过以下代码获得(x,y)值:

 eye           nose       mouse       ear
  (34, 35)      (45,66)    (45,64)     (78,87)
  (35, 38)      (75,76)    (95,37)     (38,79)
  (64, 43)      (85,66)    (65,45)     (87,45)

你知道吗

def process_xy(val_str):
    s = val_str.split('_')
    x = float(s[0])
    y = float(s[1])
    label = int(s[2])
    return np.array([x, y])

keypoint_cols = list(df.columns)
d = None
for col in keypoint_cols:
    df[col+'_xy'] = df[col].apply(process_xy)

df2 = df.drop(keypoint_cols, axis=1)

Tags: 数据dfcolvalfloatprocessnose数据类型
3条回答

您可以再次尝试stackunstacking。你知道吗

v = df.stack().str.split('_', expand=True).iloc[:, :-1]
v.columns = ['x', 'y']

v = v.unstack().swaplevel(0, 1, axis=1)
v.columns = v.columns.map('_'.join)

v.sort_index(axis=1)

  ear_x ear_y eye_x eye_y mouse_x mouse_y nose_x nose_y
0    78    87    34    35      45      64     45     66
1    38    79    35    38      95      37     75     76
2    87    45    64    43      65      45     85     66

这里有一种使用列表理解和pd.concat的方法。你知道吗

res = pd.concat([df[col].str.split('_', expand=True).iloc[:, :2].add_prefix(col) \
                for col in df], axis=1).astype(int)

我将列后缀重命名作为练习。你知道吗

结果

  eye0 eye1 nose0 nose1 mouse0 mouse1 ear0 ear1
0   34   35    45    66     45     64   78   87
1   35   38    75    76     95     37   38   79
2   64   43    85    66     65     45   87   45

解释

  • 使用pd.concataxis=1沿列聚合每个类别。你知道吗
  • _分割值,使用expand=True并只取前2个分量。你知道吗
  • 使用pd.DataFrame.astype转换为int。你知道吗

我将用stackapply(pd.Series)做什么str.split

s=df.apply(lambda x : x.str.split('_')).stack().apply(pd.Series)# convert to list then unnesting it 
s=s.apply(pd.to_numeric,errors='coerce').dropna(1).rename(columns={0:'x',1:'y'}).unstack() # apply the numeric check , drop the na
s.columns=s.columns.map('{0[1]}_{0[0]}'.format)# change multiple column to flatten 
s
Out[1274]: 
   eye_x  nose_x  mouse_x  ear_x  eye_y  nose_y  mouse_y  ear_y
0     34      45       45     78     35      66       64     87
1     35      75       95     38     38      76       37     79
2     64      85       65     87     43      66       45     45

相关问题 更多 >