简体   繁体   中英

how do I perform the below operation in python

Sorry probably av basic Q but struggling with it. I have nine variables Q1 through Q9 with initial values equal to zero.

Q1 = Q2 = Q3 = Q4 = Q5 = Q6 = Q7 = Q8 = Q9 = 0

after certain operation I get new values assigned to some of the variables.

lets assume I assign values to some of the variables randomly as shown below
pd.DataFrame((np.arange(6)+1),index=['Q1','Q2','Q5','Q8','Q9','Q4'])

So variables 'Q3','Q6' and 'Q7' are unchanged.

Question is how do I get a final output in the form of a dataframe similar to shown below. ie with new values where applicable else the original value ie 0

在此处输入图像描述

also, just to add any number of variables could have new values during the operation. ie in the above example 6 variables got new values, in some cases none might have new values or all 9 might have new values.

reindex

Docs

Qs = [f'Q{i}' for i in range(1, 10)]
df.reindex(Qs, fill_value=0)

    0
Q1  1
Q2  2
Q3  0
Q4  6
Q5  3
Q6  0
Q7  0
Q8  4
Q9  5

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