If I have the following pandas
DataFrame
:
>>> df
x y z
x 1 3 0
y 0 5 0
z 0 3 4
I want to iterate over the pairwise combinations of column names and row indices to perform certain operation. For example, for the pair of x
and y
, replace the 3 with 'xy'. The desired output will look like:
>>> df
x y z
x xx xy xz
y xy yy yz
z xz yz zz
a naïve code that I tried and doesn't work is:
for i, j in range(0,2):
df.loc[df.index[i], df.columns[j]] = df.index[i] + df.columns[j]