1

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.

2
  • are you going to reverse the list before you use it to check the df? Your desired output is off from what you are trying to do. Commented Feb 18, 2022 at 19:09
  • my apologies, yes, the list needs to be reversed in this case. Ideally, I would not be reversing the list though and would keep indexing the same. is there a quick way to accomplish that? Commented Feb 18, 2022 at 19:14

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Amirhossein, the code does the job. I thought something should be done with dictionary to solve the problem. anyway, I've decided to accept solution below which looks much cleaner. Thanks!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.