Paradox Paradox - 1 year ago 129
Python Question

Python co-occurrence matrix, how to multiply row/column by integer?

I have a co-occurrence matrix of the following form:

Cat Dog Zebra
Cat 0 2 3
Dog 2 0 1
Zebra 3 1 0

which has been generated with the python pandas library. Let's say that the its data frame is stored in the variable
and I have another integer variable
. Then how could I multiply all values for an animal by my variable

For example, if my variable x = 2 and animal = Dog, then the matrix would look like this:

Cat Dog Zebra
Cat 0 4 3
Dog 4 0 2
Zebra 3 2 0

Answer Source

To multiply a row:

df.loc['Dog', :] = df.loc['Dog', :] * x

To multiply a column:

df['Dog'] = df['Dog'] * x

See this page for more info:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download