Home > OS >  how to replace the same value in pandas dataframe with a different value in each row
how to replace the same value in pandas dataframe with a different value in each row

Time:02-19

I want to replace value 0 in each row in the pandas dataframe with a value that comes from a list that has the same index as the row index of the dataframe.

# here is my dataframe
df = pd.DataFrame({'a': [12, 52, 0], 'b': [33, 0, 110], 'c':[0, 15, 134]})

#here is the list
maxValueInRow = [3,5,34]

# the desired output would be:
df_updated = pd.DataFrame({'a': [12, 52, 3], 'b': [33, 5, 110], 'c':[34, 15, 134]})

I thought it could be something like

df.apply(lambda row: maxValueInRow[row.name] if row==0 else row, axis=1)

but that didnt work and produced 'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' error. Any thoughts would be greatly appreciated.

CodePudding user response:

Here is what you need:

# here is my dataframe
df = pd.DataFrame({'a': [12, 52, 0], 'b': [33, 0, 110], 'c':[0, 15, 134]})

#here is the list
maxValueInRow = [3,5,34]
for index, row in df.iterrows():
  for column in df.columns:
    if row[column] == 0:
      df.iloc[index][column] = maxValueInRow[index]
df

Output

a b c
0 12 33 3
1 52 5 15
2 34 110 134

Update

As per your comments, it seems by replacing the values with the same index, you meant something else. Anyway, here is an update to your problem:

# here is my dataframe
df = pd.DataFrame({'a': [12, 52, 0], 'b': [33, 0, 110], 'c':[0, 15, 134]})
data = df.to_dict()
maxValueInRow = [3,5,34]
i = 0
for chr, innerList in data.items():
  for index in range(len(innerList)):
    value = innerList[index]
    if value == 0:
      data[chr][index] = maxValueInRow[i]
  i  = 1
df = pd.DataFrame(data)
df

Output

a b c
0 12 33 34
1 52 5 15
2 3 110 134

CodePudding user response:

You could use .replace:

df = pd.DataFrame({'a': [12, 52, 0], 'b': [33, 0, 110], 'c':[0, 15, 134]})
maxValueInRow = [3,5,34]

repl = {col: {0: value} for col, value in zip(df.columns, maxValueInRow)}
df_updated = df.replace(repl)

Result:

    a    b    c
0  12   33   34
1  52    5   15
2   3  110  134
  • Related