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'])
0

1 Answer 1

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

4 Comments

Is it possible without the column name?
Could you be more specific? Do you want to reference the column by index, or would you like to perform the operation on the entire dataframe?
entire dataframe as column names can change so don't want to be dependent on column names.
Try this for column index 2: `df.loc[:,2]'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.