Check if list cell contains value

alexgg94

Having a dataframe like this:

  month  transactions_ids 
0     1         [0, 5, 1]        
1     2            [7, 4]    
2     3    [8, 10, 9, 11]     
3     6               [2]                  
4     9               [3]  

For a given transaction_id, I would like to get the month when it took place. Notice that a transaction_id can only be related to one single month.

So for example, given transaction_id = 4, the month would be 2.

I know this can be done in a loop by looking month by month if the transactions_ids related contain the given transaction_id, but I'm wondering if there is any way more efficient than that.

Cheers

rafaelc

The best way in my opinion is to explode your data frame and avoid having python lists in your cells.

df = df.explode('transaction_ids')

which outputs

   month transactions_ids
0      1                0
0      1                5
0      1                1
1      2                7
1      2                4
2      3                8
2      3               10
2      3                9
2      3               11
3      6                2
4      9                3

Then, simply

id_to_find = 1 # example
df.loc[df.transactions_ids == id_to_find, 'month']

P.S: be aware of the duplicated indexes that explode outputs. In general, it is better to do explode(...).reset_index(drop=True) for most cases to avoid unwanted behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check if cell contains text from a list and display that value

From Dev

VBA to check if cell contains anything from a list

From Dev

Check if a list of strings contains a value

From Dev

How to check if a list of booleans contains a value?

From Dev

Check a List contains the same value multiple times

From Dev

How to check if a list of booleans contains a value?

From Dev

Check a List contains the same value multiple times

From Dev

How to check if List contains part of a value?

From Dev

Check if a cell contains any of words from a dynamic list

From Dev

Check if cell contains sub string in list - Excel 2007

From Dev

Python pandas check if the last element of a list in a cell contains specific string

From Java

Check whether a cell contains a substring

From Dev

Check if a cell contains specific text

From Dev

Check if a cell contains specific text

From Java

xslt 1 : check if node contains any value from list

From Dev

Check if string list contains any enum string value

From Dev

Why are a list of short objects not able to check if it contains the value or not

From Dev

Check if dictionary contains at least one specific value out of a list

From Dev

Pandas check if dataframe column contains value from list (different lengths)

From Dev

Check if list contains a type?

From Dev

Check if element is in the list (contains)

From Dev

Check if heapq contains value

From Dev

Check if heapq contains value

From Dev

If cell contains value then 'Column header'

From Dev

If cell contains partial string, then change cell value

From Dev

insert cell next to a cell that contains a certain value

From Dev

How do I check if a cell only contains characters from a specific list?

From Dev

How do I check if a cell only contains characters from a specific list?

From Dev

In Excel, how do i check if cell contains at least twice the same character from a list

Related Related

  1. 1

    Check if cell contains text from a list and display that value

  2. 2

    VBA to check if cell contains anything from a list

  3. 3

    Check if a list of strings contains a value

  4. 4

    How to check if a list of booleans contains a value?

  5. 5

    Check a List contains the same value multiple times

  6. 6

    How to check if a list of booleans contains a value?

  7. 7

    Check a List contains the same value multiple times

  8. 8

    How to check if List contains part of a value?

  9. 9

    Check if a cell contains any of words from a dynamic list

  10. 10

    Check if cell contains sub string in list - Excel 2007

  11. 11

    Python pandas check if the last element of a list in a cell contains specific string

  12. 12

    Check whether a cell contains a substring

  13. 13

    Check if a cell contains specific text

  14. 14

    Check if a cell contains specific text

  15. 15

    xslt 1 : check if node contains any value from list

  16. 16

    Check if string list contains any enum string value

  17. 17

    Why are a list of short objects not able to check if it contains the value or not

  18. 18

    Check if dictionary contains at least one specific value out of a list

  19. 19

    Pandas check if dataframe column contains value from list (different lengths)

  20. 20

    Check if list contains a type?

  21. 21

    Check if element is in the list (contains)

  22. 22

    Check if heapq contains value

  23. 23

    Check if heapq contains value

  24. 24

    If cell contains value then 'Column header'

  25. 25

    If cell contains partial string, then change cell value

  26. 26

    insert cell next to a cell that contains a certain value

  27. 27

    How do I check if a cell only contains characters from a specific list?

  28. 28

    How do I check if a cell only contains characters from a specific list?

  29. 29

    In Excel, how do i check if cell contains at least twice the same character from a list

HotTag

Archive