简体   繁体   中英

Divide Pandas Dataframe columns by factors defined in dictionary

I have a Pandas dataframe with columns of numbers. I would like to divide each column by a unique number as defined in a dictionary mapping the column name to the factor.

I was able to get the outcome I wanted by using a for loop, but I suspect that Pandas must have a built-in way of handling this.

df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

df.head()
   A  B  C  D
0  9  8  1  1
1  9  7  1  6
2  0  6  7  5
3  5  1  6  0
4  4  0  5  4

factors = {'A':1, 'B':2, 'C':3, 'D':1}

for col_name, factor in factors.items():
   df[col_name] = df[col_name]/factor

Try:

df.div(pd.Series(factors))

Pandas always aligns indexes before any computation.

As a side note, it pays to have a random seed to enable reproducible data.

np.random.seed(4)
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

df.div(pd.Series(factors))

     A    B         C    D
0  7.0  2.5  0.333333  8.0
1  7.0  4.0  0.666667  9.0
2  7.0  3.5  2.333333  9.0
3  8.0  2.0  0.666667  6.0
4  4.0  1.5  0.000000  7.0
5  5.0  2.5  3.000000  6.0
6  6.0  4.0  0.666667  5.0
7  8.0  0.5  0.666667  7.0
8  0.0  4.0  1.000000  1.0
9  0.0  1.5  0.666667  3.0

As @hootnot noted, simpy running df/factors works as well:

df/factors
 
     A    B         C    D
0  7.0  2.5  0.333333  8.0
1  7.0  4.0  0.666667  9.0
2  7.0  3.5  2.333333  9.0
3  8.0  2.0  0.666667  6.0
4  4.0  1.5  0.000000  7.0
5  5.0  2.5  3.000000  6.0
6  6.0  4.0  0.666667  5.0
7  8.0  0.5  0.666667  7.0
8  0.0  4.0  1.000000  1.0
9  0.0  1.5  0.666667  3.0

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM