我有一个csv文件,看起来像这样。我已将文件加载为熊猫数据框。有什么办法可以使time列中的所有值都在同一范围内,即所有条目以毫秒为单位,内存以MB为单位?
prefix/up17/strided_slice_1 4B 15us
prefix/fres16/conv_b_1x3/weights 196.61KB 12us
prefix/fres16/conv_a_3x1/weights 196.61KB 12us
prefix/up20/weights 65.54KB 12us
prefix/fres15/conv_b_1x3/weights 196.61KB 11us
prefix/fres18/conv_b_3x1/weights 49.15KB 11us
prefix/fres7/conv_b_3x1/weights 49.15KB 11us
prefix/fres5/conv_a_3x1/weights 49.15KB 11us
prefix/fres13/conv_b_1x3/weights 196.61KB 11us
prefix/fres18/conv_b_1x3/weights 49.15KB 11us
prefix/fres5/conv_a_1x3/weights 49.15KB 11us
prefix/fres14/conv_a_3x1/weights 196.61KB 11us
prefix/fres7/conv_a_1x3/weights 49.15KB 11us
prefix/fres10/conv_b_1x3/weights 196.61KB 11us
prefix/fres14/conv_b_1x3/weights 196.61KB 11us
prefix/fres4/conv_b_3x1/weights 49.15KB 11us
prefix/d2/conv/weights 27.65KB 11us
prefix/fres19/conv_a_1x3/weights 49.15KB 11us
prefix/fres13/conv_a_1x3/weights 196.61KB 11us
prefix/fres11/conv_a_1x3/weights 196.61KB 11us
prefix/fres21/conv_b_3x1/weights 3.07KB 11us
prefix/fres12/conv_b_3x1/weights 196.61KB 11us
prefix/fres22/conv_b_3x1/weights 3.07KB 11us
prefix/fres18/conv_a_1x3/weights 49.15KB 11us
prefix/fres9/conv_a_3x1/weights 196.61KB 11us
很抱歉,这个问题很幼稚。我是熊猫新手。
答案 0 :(得分:2)
有趣的问题。假设相关列分别命名为size
和time
,这就是我的处理方式:
size_data = df['size'].str.extract('([\d\.]+)\B(\w?B)').rename(columns={0: 'size_amount', 1: 'size_multiplier'})
time_data = df['time'].str.extract('([\d\.]+)\B(\w?s)').rename(columns={0: 'time_amount', 1: 'time_multiplier'})
size_mapping = {'B': 1 / (1024 ** 2), 'KB': 1 / 1024, 'MB': 1}
time_mapping = {'ms': 1, 'us': 1e-3}
df['size'] = size_data ['size_amount'].astype(float) * size_data ['size_multiplier'].map(size_mapping)
df['time'] = time_data ['time_amount'].astype(float) * time_data ['time_multiplier'].map(time_mapping)
print(df)
输出:
path size time
0 prefix/up17/strided_slice_1 0.000004 0.015
1 prefix/fres16/conv_b_1x3/weights 0.192002 0.012
2 prefix/fres16/conv_a_3x1/weights 0.192002 0.012
3 prefix/up20/weights 0.064004 0.012
4 prefix/fres15/conv_b_1x3/weights 0.192002 0.011
5 prefix/fres18/conv_b_3x1/weights 0.047998 0.011
6 prefix/fres7/conv_b_3x1/weights 0.047998 0.011
7 prefix/fres5/conv_a_3x1/weights 0.047998 0.011
8 prefix/fres13/conv_b_1x3/weights 0.192002 0.011
9 prefix/fres18/conv_b_1x3/weights 0.047998 0.011
10 prefix/fres5/conv_a_1x3/weights 0.047998 0.011
11 prefix/fres14/conv_a_3x1/weights 0.192002 0.011
12 prefix/fres7/conv_a_1x3/weights 0.047998 0.011
13 prefix/fres10/conv_b_1x3/weights 0.192002 0.011
14 prefix/fres14/conv_b_1x3/weights 0.192002 0.011
15 prefix/fres4/conv_b_3x1/weights 0.047998 0.011
16 prefix/d2/conv/weights 0.027002 0.011
17 prefix/fres19/conv_a_1x3/weights 0.047998 0.011
18 prefix/fres13/conv_a_1x3/weights 0.192002 0.011
19 prefix/fres11/conv_a_1x3/weights 0.192002 0.011
20 prefix/fres21/conv_b_3x1/weights 0.002998 0.011
21 prefix/fres12/conv_b_3x1/weights 0.192002 0.011
22 prefix/fres22/conv_b_3x1/weights 0.002998 0.011
23 prefix/fres18/conv_a_1x3/weights 0.047998 0.011
24 prefix/fres9/conv_a_3x1/weights 0.192002 0.011
如果要添加其他单位或更改基本单位,只需修改size_mapping
和/或time_mapping
。