Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

Formatting the data of multiple columns in a dataframe

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes , you can do it like below:

for cols in df[['avg', 'max']].columns:
    df[cols] = df[cols].apply(function)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...