I've data frame that has several columns. A couple of columns have a very large values and I want to convert those into kilo, mega, giga format (eg: 10m, 20g etc). Got a custom function to do it but kind of stuck on how to apply it for selected columns. This is what I tried:
Df:
key avg max
--- --- ----
speed 100000 200000000
...
def convert(x: str):
if int(x) <= 0:
return 0
.. do conversion here
df.apply(apply(lambda x: convert if x.name in ['avg', 'max'] else x)
However, this doesn't seem to be doing the conversion and also giving an error message about the data type. The following works but it has to be done for each field separately.
df['avg'] = df['avg'].apply(func)
Is there a better way to do this?
question from:
https://stackoverflow.com/questions/65838363/formatting-the-data-of-multiple-columns-in-a-dataframe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…