简体   繁体   中英

How to create a DataFrame in which last column (i.e. fifth column) will be the addition of first four column

The first four columns are randomly generated between a certain range. The fifth column contains the addition of the first four columns. The value of the fifth column should not be more than 10.

Not sure if I understand well:

import numpy as np
import pandas as pd

A=np.random.randint(0.5,5,10)
B=np.random.randint(.50,10,10)
C=np.random.randint(0.8,8,10)
D=np.random.randint(0.1,2,10)
E=A+B+C+D
df={'A':A,'B':B, 'C':C,'E':E}
df= pd.DataFrame(df)
df['E'].values[df['E'] > 10] = 0 # if E bigger than 10

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