ClementWalter
ClementWalter

Reputation: 5272

How to efficiently set values in a pandas dataframe according to a list of coordinates?

I have a 2d dataframe and a list of coordinates that I want to use to set the values in my df.

I have a done a basic for loop but it seams unefficient to me.

MWE:

import pandas as pd
df = pd.DataFrame(pd.np.ones((3, 4)))
coordinates = [(2, 1), (2, 3)]
for point in coordinates:
    df.iloc[point[0], point[1]] = pd.np.inf

print(df)
     0         1    2         3
0  1.0  1.000000  1.0  1.000000
1  1.0  1.000000  1.0  1.000000
2  1.0       inf  1.0       inf

Is there a way to do something like df.iloc[coordinates] = pd.np.inf ?

Upvotes: 0

Views: 561

Answers (1)

sjw
sjw

Reputation: 6543

Borrowing heavily from this answer, you can operate directly on the underlying numpy array. I'm not aware of any built-in pandas function which does what you are asking.

import pandas as pd

df = pd.DataFrame(pd.np.ones((3, 4)))
coordinates = [(2, 1), (2, 3)]
rows, cols = zip(*coordinates)
df.values[rows, cols] = pd.np.inf

Results:

     0         1    2         3
0  1.0  1.000000  1.0  1.000000
1  1.0  1.000000  1.0  1.000000
2  1.0       inf  1.0       inf

Upvotes: 5

Related Questions