Home > Software engineering >  Updating Pandas data fram cells by condition
Updating Pandas data fram cells by condition

Time:01-01

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?

CodePudding user response:

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
  • Related