I have a pandas dataframe in which I want to divide each column by the same data series values for each row. Each column and the data series have the same length. The data series has only float numbers but some cells in the dataframe have NaNs.
I tried various things but somehow I cannot get it solved.
df = df.divide(data_series, axis=1)
Any suggestions?
many thanks!
I found the solution
Setting axis = 0 solves the issue.
df = df.divide(data_series, axis=0 )
many thanks
Format your Series
index with df columns
df.div(pd.Series([100,100,100],index=df.columns))
Out[132]:
A B id
0 0.01 0.02 0.01
1 0.02 0.03 0.01
2 0.04 0.00 0.03
3 0.05 0.06 0.01
4 0.08 0.09 0.03
5 0.09 0.07 0.03
Given dataframe df
and series s
, you can also use numpy
:
res = pd.DataFrame(df.values / s.values[:, None],
index=df.index, columns=df.columns)
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.