合并pandas中的float列

时间:2016-05-24 21:02:56

标签: pandas

我有一个pandas数据框df,如下所示:

Index   count    mean     std
1       101.0    8.15    3.26
2       102.0   13.20   11.48
3       101.0  235.15  186.76
4        84.0  420.49  637.59

我需要合并'mean'列和'std'列,以便我有一个如下所示的数据框:

Index   count    mean(std)    
1       101.0    8.15 (3.26)
2       102.0   13.20 (11.48)
3       101.0  235.15 (186.76)
4        84.0  420.49 (637.59)

我怎样才能实现这个是熊猫?

2 个答案:

答案 0 :(得分:1)

我会这样做:

df['mean'] = df['mean'].map(str) + ' (' + df['std'].map(str) + ')'
df = df.drop(['std'], axis=1).rename(columns={'mean':'mean(std)'})

定时:

In [425]: df = pd.concat([df]*10**5)

In [426]: df.shape
Out[426]: (400000, 3)

In [427]: %timeit df['mean'].map(str) + '( ' + df['std'].map(str) + ')'
1 loop, best of 3: 786 ms per loop

In [428]: %timeit df['mean'].map('{:,.2f}'.format) + df['std'].map('({:,.2f})'.format)
1 loop, best of 3: 845 ms per loop

In [429]: %timeit df['mean'].astype(str) + '(' + df['std'].astype(str) + ')'
1 loop, best of 3: 1.51 s per loop

结论:

map似乎比astype

更快

答案 1 :(得分:0)

转换为字符串并合并:

df['mean(std)'] = df['mean'].map('{:,.2f}'.format) + df['std'].map('({:,.2f})'.format)

print(df)      
        count    mean     std       mean(std)
Index
1        101    8.15    3.26      8.15(3.26)
2        102   13.20   11.48    13.20(11.48)
3        101  235.15  186.76  235.15(186.76)
4         84  420.49  637.59  420.49(637.59)