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

Updating Pandas data fram cells by condition

I have a data frame and want to update specific cells in a column based on a condition on another column.

   ID Name Metric     Unit  Value
2   1   K2     M1  msecond      1
3   1   K2     M2      NaN     10
4   2   K2     M1  usecond    500
5   2   K2     M2      NaN      8

The condition is, if Unit string is msecond, then multiply the corresponding value in Value column by 1000 and store it in the same place. Considering a constant step for row iteration (two-by-two), the following code is not correct

i = 0
while i < len(df_group):
    x = df.iloc[i].at["Unit"]
    if x == 'msecond':
        df.iloc[i].at["Value"] = df.iloc[i].at["Value"] * 1000
    i += 2

However, the output is the same as before modifications. How can I fix that? Also what are the alternatives for better coding instead of that while loop?

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

>Solution :

A much simpler (and more efficient) form would be to use loc:

df.loc[df['Unit'] == 'msecond', 'Value'] *= 100

If you consider it essentially to only update a specific step of indexes:

step = 2
start = 0
df.loc[df['Unit'].eq('msecond') & (df.index % step == start), 'Value'] *= 100
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