我可以为下面的操作使用什么更好的语法?A 列的变量包含 2 个具有不同转换方法的不同 SI 单位:KB 和 MB。因此,我想对 pandas 数据框中的列执行以下操作: data = pd.dataframe({"sizes":[12mb, 7kb, 17kb, 11mn], "song-type":["karaoke" , “摇滚”, “爵士”, “说唱”]}, 索引 = [1,2,3,4])
我想从大小中提取数字并将具有“mb”的变量的每个数字分量乘以 1000。然后将结果设为 int 或 float 格式
if 'M' in x:
extract the numerics in x
convert to float
and assign x to (x * by 1000)
elif 'K' in x:
extract the numerics in x and convert to float
and assign it to x
else:
extract the numerics in x and convert to float
and assign it to x