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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With