Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change dataframe cells values with "coordinate-like" indexes stored in two lists/vectors/series?

Tags:

python

pandas

Apologize if this has been asked before, somehow I am not able to find the answer to this.

Let's say I have two lists of values:

rows = [0,1,2]
cols = [0,2,3]

that represents indexes of rows and columns respectively. The two lists combined signified sort of coordinates in the matrix, i.e (0,0), (1,2), (2,3).

I would like to use those coordinates to change specific cells of the dataframe without using a loop.

In numpy, this is trivial:

data = np.ones((4,4))
data[rows, cols] = np.nan

array([[nan,  1.,  1.,  1.],
      [ 1.,  1., nan,  1.],
      [ 1.,  1.,  1., nan],
      [ 1.,  1.,  1.,  1.]])

But in pandas, it seems I am stuck with a loop:

df = pd.DataFrame(np.ones((4,4)))
for _r, _c in zip(rows, cols): 
    df.iat[_r, _c] = np.nan

Is there a way to use to vectors that lists coordinate-like index to directly modify cells in pandas?


Please note that the answer is not to use iloc instead, this selects the intersection of entire rows and columns.

like image 932
toto_tico Avatar asked Aug 21 '18 13:08

toto_tico


People also ask

How do I change the values in pandas series based on conditions?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do you change a particular cell value in a DataFrame?

if we want to modify the value of the cell [0,"A"] u can use one of those solution : df. iat[0,0] = 2. df.at[0,'A'] = 2.


1 Answers

Very simple! Exploit the fact that pandas is built on top of numpy and use DataFrame.values

df.values[rows, cols] = np.nan

Output:

     0    1    2    3
0  NaN  1.0  1.0  1.0
1  1.0  1.0  NaN  1.0
2  1.0  1.0  1.0  NaN
3  1.0  1.0  1.0  1.0
like image 196
Yuca Avatar answered Oct 24 '22 18:10

Yuca