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

This question has the answer Update a dataframe in pandas while iterating row by row but it depends on the column name. I would like to have a solution which only depends on values and not based on column name.

user1298426
  • 3,467
  • 15
  • 50
  • 96
  • [`DataFrame.replace`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html) ? – sushanth Aug 18 '20 at 12:48

3 Answers3

0

You can get the column names dynamic by index and use the result as in the solution that you refer to.

A way to get the name by index is use this line:

# Import pandas package  
import pandas as pd  
    
# making data frame  
df = pd.read_csv("your_data.csv")  
    
# get column names
col_name_list = list(df.columns)
tifi90
  • 403
  • 4
  • 13
0

Here, j is a series. So, you can easily navigate to your preferred column using iloc. You don't even need to care whether you have used MultiIndex or not. Hence,

for i, j in df1.iterrows():
    print(j['Name with Name Text']['Field1'])

is equivalent to

for i, j in df1.iterrows():
    print(j.iloc[2])

2 is used here because it is the third column.

Edit: If you want to iterate through every columns:

for i, j in df1.iterrows():
    for k in range(len(df1.columns)):
        print(j.iloc[k])
Promit Basak
  • 161
  • 4
  • is it possible to see how many columns are there and then iterate over it incrementally iso of hard coding 2? – user1298426 Aug 18 '20 at 13:38
  • Yes, you can get the number of columns from `len(df1.columns)` and insert a for loop to iterate over every columns. I am editing the answer. You may look at that. – Promit Basak Aug 18 '20 at 13:57
0

When iterating on the rows, each row is a Series that you can also iterate:

for i, row in df.iterrows():
    for col, value in row.iteritems():
        print(value)

If you also want to change the value you can use:

df.set_value(i, col, <some_value>)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61