Select a certain value from a pandas dense rank dataframe

E.K.

I have a pandas dataframe with dense rank matrix and want to select all the cells that have 2. And then transform it to results dataframe like below. I am looping through each column and row with just for-loop but is there a better way?

df looks like

    A   B   C  ........ x 2000 columns 
AA  1   3   2
BB  2   1   3
CC  2   2   1
 .
 .
 .
 x
2000 rows

results_df to be like

    Col1  Col2
0   A     BB
1   A     CC
2   B     CC
3   C     AA
ely

Here is one method.

rows, cols = np.nonzero((df==2).values)

results_df = pandas.DataFrame({
    'Col1':[df.columns[c] for c in cols], 
    'Col2':[df.index[r] for r in rows]
}).sort('Col1').reset_index(drop=True)

For example:

In [88]: df
Out[88]: 
    A  B  C
AA  1  3  2
BB  2  1  3
CC  2  2  1

In [89]: pandas.DataFrame({'Col1':[df.columns[c] for c in cols], 'Col2':[df.index[r] for r in rows]}).sort('Col1').reset_index(drop=True)
Out[89]: 
  Col1 Col2
0    A   BB
1    A   CC
2    B   CC
3    C   AA

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select rows from pandas DataFrame of a certain date

From Dev

Select rows containing certain values from pandas dataframe

From Dev

Delete Rows from a pandas dataframe if matching strings exceed a certain value

From Dev

Second maximum value by using dense rank function

From Dev

Rank or Dense_Rank

From Dev

Check if certain value is contained in a dataframe column in pandas

From Dev

Pandas rank by column value

From Dev

Select Days from Pandas DataFrame

From Dev

assign value from one dataframe to another by rank using loop

From Dev

MS Access equivalent for using dense_rank in select

From Dev

Extracting all rows from pandas Dataframe that have certain value in a specific column

From Dev

Python Pandas: Eliminate a row from a dataframe if a value in a any preceding row in a groupby meets a certain criteria

From Dev

Use of keep dense_rank to find a single value

From Dev

Sequential number and maximum value using DENSE_RANK

From Dev

Use of keep dense_rank to find a single value

From Java

Pivoting pandas dataframe by rank on id

From Dev

Pandas rank valus in rows of DataFrame

From Dev

Pandas Dataframe delete row with certain value until that value changes

From Dev

R, select rows according to the rank of a certain column

From Dev

R, select rows according to the rank of a certain column

From Dev

Python[pandas]: Select certain rows by index of another dataframe

From Dev

Convert keep dense_rank from Oracle query into postgres

From Dev

Select columns in pandas dataframe by value in rows

From Dev

Pandas rank by column value with conditions

From Dev

Dense Rank with paging and order by

From Dev

implement dense rank with linq

From Dev

Dense_rank and sum

From Dev

Dense Rank in Excel

From Dev

Dense_Rank ordering

Related Related

  1. 1

    Select rows from pandas DataFrame of a certain date

  2. 2

    Select rows containing certain values from pandas dataframe

  3. 3

    Delete Rows from a pandas dataframe if matching strings exceed a certain value

  4. 4

    Second maximum value by using dense rank function

  5. 5

    Rank or Dense_Rank

  6. 6

    Check if certain value is contained in a dataframe column in pandas

  7. 7

    Pandas rank by column value

  8. 8

    Select Days from Pandas DataFrame

  9. 9

    assign value from one dataframe to another by rank using loop

  10. 10

    MS Access equivalent for using dense_rank in select

  11. 11

    Extracting all rows from pandas Dataframe that have certain value in a specific column

  12. 12

    Python Pandas: Eliminate a row from a dataframe if a value in a any preceding row in a groupby meets a certain criteria

  13. 13

    Use of keep dense_rank to find a single value

  14. 14

    Sequential number and maximum value using DENSE_RANK

  15. 15

    Use of keep dense_rank to find a single value

  16. 16

    Pivoting pandas dataframe by rank on id

  17. 17

    Pandas rank valus in rows of DataFrame

  18. 18

    Pandas Dataframe delete row with certain value until that value changes

  19. 19

    R, select rows according to the rank of a certain column

  20. 20

    R, select rows according to the rank of a certain column

  21. 21

    Python[pandas]: Select certain rows by index of another dataframe

  22. 22

    Convert keep dense_rank from Oracle query into postgres

  23. 23

    Select columns in pandas dataframe by value in rows

  24. 24

    Pandas rank by column value with conditions

  25. 25

    Dense Rank with paging and order by

  26. 26

    implement dense rank with linq

  27. 27

    Dense_rank and sum

  28. 28

    Dense Rank in Excel

  29. 29

    Dense_Rank ordering

HotTag

Archive