Suppose that you have a pandas DataFrame
which has some kind of data in the body and numbers in the column
and index
names.
>>> data=np.array([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']])
>>> columns = [2, 4, 8]
>>> index = [10, 4, 2]
>>> df = pd.DataFrame(data, columns=columns, index=index)
>>> df
2 4 8
10 a b c
4 d e f
2 g h i
Now suppose we want to manipulate are data frame in some kind of way based on comparing the index and columns. Consider the following.
Where index is greater than column replace letter with 'k':
2 4 8
10 k k k
4 k e f
2 g h i
Where index is equal to column replace letter with 'U':
2 4 8
10 k k k
4 k U f
2 U h i
Where column is greater than index replace letter with 'Y':
2 4 8
10 k k k
4 k U Y
2 U Y Y
To keep the question useful to all:
What is a fast way to do this replacement?
What is the simplest way to do this replacement?
Speed Results from minimal example
jezrael:
556 µs ± 66.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
user3471881:
329 µs ± 11.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
thunderwood:
4.65 ms ± 252 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Is this a duplicate?
I searched google for pandas replace compare index column
and the top results are:
Pandas - Compare two dataframes and replace values matching condition
Python pandas: replace values based on location not index value
Pandas DataFrame: replace all values in a column, based on condition
However, I don't feel any of these touch on whether this a) possible or b) how to compare in such a way