0

I have a dataframe which has columns like below:

MultiIndex([('Longitude', 'Field1'),
            ('Latitude', 'Field1'),
            ('Name with Name Text', 'Field1'),
            ('Name with Name Text', 'Field2'),
            ('IsPresent', 'Field1')],)

A csv example of this would be

enter image description here

I want to iterate through each row in the data and for each value of field I want to replace certain characters, if they exist. For example, replace 'X' with 'Y'. But I don't want to depend on the column name as it can change. I want to iterate over each value and strip the character. Below iteration has field names so can't use it.

for i, j in df1.iterrows():
    print(j['Name with Name Text']['Field1'])
user1298426
  • 3,467
  • 15
  • 50
  • 96

1 Answers1

0

Although you can of course iterate row by row as described in the comment above, you can also try to find a function that does what you want on the entire column "at once".

For example, replace all "Y" to "Z" in column "col1"

    df["col1"] = df["col1"].apply(lambda x: x.replace("Y", "Z"))
Dustin
  • 483
  • 3
  • 13