Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change cell values of some specific rows without using for loop? python pandas

I have a dataframe df like this:

       symbol       P        Q          V   ...                                                 
0       00001     10.9      100         100     ...
1       00001     11.0      100         200     ... 
2       00001     11.0      100         200     ...     
3       00002     12.1     -100        -100     ...    
...       ...       ...         ...                                                       
46      00004     6.9       300         300     ...
47      00004     7.0      -200         100     ...    
48      00004     8.8       100         200     ...    
49      00004     8.8       100         200     ... 

I also have an index list idx=[2, ... 49]. I would like to change the value of rows which indexings are in this list. For instance, I want to set

df.loc[i,'Q'] = df.loc[i-1,'V']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, this kind of command threw some errors because it requires a specific index instead of an index list. I tried to use a for loop like this:

for i in idx:
    df.at[i, "Q"] = df.at[i-1, "V"]

Although it works, the speed would be slow down due to the for loop if the dataset is large. Is there any pandas command that could assign new values to a column of multiple rows based on an index list, like above?

>Solution :

If you convert idx to a numpy array, it could work:

idx = [2, 3, 48, 49]
idx = np.array(idx)

# Don't forget .values to ignore index
df.loc[idx, 'Q'] = df.loc[idx-1, 'V'].values

Output:

>>> df
    symbol     P    Q    V
0        1  10.9  100  100
1        1  11.0  100  200
2        1  11.0  200  200
3        2  12.1  200 -100
46       4   6.9  300  300
47       4   7.0 -200  100
48       4   8.8  100  200
49       4   8.8  200  200
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading