Pandas Dataframe row selection combined condition index- and column values

Egirus Ornila

I want to select rows from a dataframe based on values in the index combined with values in a specific column:

df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [0, 20, 30], [40, 20, 30]], 
                  index=[4, 5, 6, 7], columns=['A', 'B', 'C'])


    A   B   C
4   0   2   3
5   0   4   1
6   0  20  30
7  40  20  30

with

df.loc[df['A'] == 0, 'C'] = 99

i can select all rows with column A = 0 and replace the value in column C with 99, but how can i select all rows with column A = 0 and the index < 6 (i want to combine selection on the index with selection on the column)?

sacuL

You can use multiple conditions in your loc statement:

df.loc[(df.index < 6) & (df.A == 0), 'C'] = 99

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas Dataframe row selection combined condition index- and column values

From Dev

Pandas DataFrame get column combined max values

From Dev

Pandas DataFrame get column combined max values

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

Pandas dataframe - Rearranging row index values into column headers

From Dev

Getting the index of pandas dataframe for matching row values

From Dev

Row Values to Column Array in Pandas DataFrame

From Dev

Two dataframe columns to row and column index with third column as values

From Dev

Sum pandas dataframe column values based on condition of column name

From Dev

Efficient max selection in pandas dataframe with selection condition

From Dev

Use column values in Pandas DataFrame to populate string row by row

From Dev

Python / Pandas: Renaming several column names in DataFrame based on condition/index

From Dev

Python / Pandas: Renaming several column names in DataFrame based on condition/index

From Dev

Pandas dataframe: slicing column values using second column for slice index

From Dev

conditional row and column selection in pandas

From Java

Pandas DataFrame: replace all values in a column, based on condition

From Dev

Replace values in pandas dataframe column with different replacement dict based on condition

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

From Dev

How to construct pandas DataFrame from dictionary with key as column and row index

From Dev

Get Column and Row Index for Highest Value in Dataframe Pandas

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

From Dev

Deleting DataFrame row in a multilevel index Pandas based on column value

From Dev

Column to row in pandas dataframe

From Java

Get row-index values of Pandas DataFrame as list?

From Java

pandas dataframe groupby index and convert row values into columns

From Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

From Dev

Summing values from pandas dataframe columns depending on row index value

From Dev

Find row index of pandas dataframe that don't have finite values

Related Related

  1. 1

    Pandas Dataframe row selection combined condition index- and column values

  2. 2

    Pandas DataFrame get column combined max values

  3. 3

    Pandas DataFrame get column combined max values

  4. 4

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  5. 5

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  6. 6

    Pandas dataframe - Rearranging row index values into column headers

  7. 7

    Getting the index of pandas dataframe for matching row values

  8. 8

    Row Values to Column Array in Pandas DataFrame

  9. 9

    Two dataframe columns to row and column index with third column as values

  10. 10

    Sum pandas dataframe column values based on condition of column name

  11. 11

    Efficient max selection in pandas dataframe with selection condition

  12. 12

    Use column values in Pandas DataFrame to populate string row by row

  13. 13

    Python / Pandas: Renaming several column names in DataFrame based on condition/index

  14. 14

    Python / Pandas: Renaming several column names in DataFrame based on condition/index

  15. 15

    Pandas dataframe: slicing column values using second column for slice index

  16. 16

    conditional row and column selection in pandas

  17. 17

    Pandas DataFrame: replace all values in a column, based on condition

  18. 18

    Replace values in pandas dataframe column with different replacement dict based on condition

  19. 19

    Get column and row index pairs of Pandas DataFrame matching some criteria

  20. 20

    How to construct pandas DataFrame from dictionary with key as column and row index

  21. 21

    Get Column and Row Index for Highest Value in Dataframe Pandas

  22. 22

    Get column and row index pairs of Pandas DataFrame matching some criteria

  23. 23

    Deleting DataFrame row in a multilevel index Pandas based on column value

  24. 24

    Column to row in pandas dataframe

  25. 25

    Get row-index values of Pandas DataFrame as list?

  26. 26

    pandas dataframe groupby index and convert row values into columns

  27. 27

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  28. 28

    Summing values from pandas dataframe columns depending on row index value

  29. 29

    Find row index of pandas dataframe that don't have finite values

HotTag

Archive