I would suggest using a own function for doing that. You can access each column of the dataframe by using the dict-like notation. In addition to get the desired element by accessing the needed index/row I would use .ix
as shown below
import pandas as pd
df = pd.DataFrame({1 : np.arange(1, 6),
2 : np.arange(6, 11),
3 : np.arange(11, 16),
4 : np.arange(16, 21),
5 : np.arange(21, 26)},
index=[1, 2, 3, 4, 5])
def get_from_coords(df, x, y):
return df[x].ix[y]
So for example:
In [2]: get_from_coords(df, 2, 1)
Out[2]: 6
The docs provide detailed information about indexing pandas dataframes.
Update since I missunderstood the question as clarified in the comments:
def look_for_value(df, value):
l = []
for row in df.itertuples():
print(row)
if value in row[1:]:
# appending a tuple of the format `(index name, column name)`
l.append((row[0], df.columns[row.index(value)-1]))
return l
def look_using_generator(df, value):
return [(row[0], df.columns[row.index(value)-1]) for row in df.itertuples() if value in row[1:]]
I am iterating through all the rows of the dataframe using .itertuples()
which is faster than .iterrows()
and looking for the desired entry/value. If the value is found in the row a tuple containing the index and column name is stored to a list which is returned at the end. I provided a kind of step-by-step solution in the first function and a one-liner using a generator in list comprehension.
Edit since OP pointed out he needs to have the column and index names to change the corresponding value:
Let's say we want to find all values 6
and replace them with 66
:
for item in look_using_generator(df, 6):
df[item[0]].ix[item[1]] = 66