I have a dataframe of the following shape:
country date col1 col2 col3 col4 col5 total
The columns that start with col are floats.
I want to replace (or create a new dataframe) each value with % of total, as in, for each date and country,
new_col1[row0]=col1[row0]/total[row0]
I tried the following:
df2=df.copy()
df2.select_dtypes(include=['float64']).div(df2.total)
I get all NaNs, and there are no missing values in the dataframe.
If I do each column individually, it works just fine.
Any help appreciated!
Is this waht you are after?
df = pd.DataFrame({'Col1': [1.0, 2.0, 3.0], 'Col2': [4.0, 5.0, 6.0]}) #Data
df.dtypes#Check data format
#Outcome of check Col1 float64 Col2 float64 dtype: object
#solution
df[['diva', 'divb']] = df[['A','B']].transform(lambda x: x / x.sum(), axis=1)
Or are you after sum of rows divided by count?
df['div']=df.apply(lambda x: (x.sum()/(len(x))), axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.