user2014979
user2014979

Reputation: 419

Operating on tuples held within Pandas DataFrame column

I have the following DataFrame:

   start      end         days
0  2015-07-01 2015-07-07         (1, 2, 3, 4, 5, 6, 7)
1  2015-07-08 2015-07-14    (8, 9, 10, 11, 12, 13, 14)
2  2015-07-15 2015-07-21  (15, 16, 17, 18, 19, 20, 21)
3  2015-07-22 2015-07-28  (22, 23, 24, 25, 26, 27, 28)
4  2015-07-29 2015-08-04      (29, 30, 31, 1, 2, 3, 4)
5  2015-08-05 2015-08-11       (5, 6, 7, 8, 9, 10, 11)
6  2015-08-12 2015-08-18  (12, 13, 14, 15, 16, 17, 18)
7  2015-08-19 2015-08-25  (19, 20, 21, 22, 23, 24, 25)
8  2015-08-26 2015-09-01   (26, 27, 28, 29, 30, 31, 1)
9  2015-09-02 2015-09-08         (2, 3, 4, 5, 6, 7, 8)
10 2015-09-09 2015-09-15   (9, 10, 11, 12, 13, 14, 15)
11 2015-09-16 2015-09-22  (16, 17, 18, 19, 20, 21, 22)
12 2015-09-23 2015-09-29  (23, 24, 25, 26, 27, 28, 29)

I am interested in working with the days column containing tuples, using Pandas syntax for basic filtering does not appear to work:

df[4 in df['days'] == True]

I was hoping the above would filter the DataFrame to return the following rows, i.e. tuples containing 4:

       start      end             days
    0  2015-07-01 2015-07-07         (1, 2, 3, 4, 5, 6, 7)
    4  2015-07-29 2015-08-04      (29, 30, 31, 1, 2, 3, 4)
    9  2015-09-02 2015-09-08         (2, 3, 4, 5, 6, 7, 8)

Instead an empty DataFrame is returned.

I have also tried creating a new column to hold True/False values based on checking against an expression like so:

df['daysTF'] = 4 in df['days']

This returns the DataFrame with the 'daysTF' column set to True for all rows, instead of only True if 4 is contained within the tuple.

Upvotes: 2

Views: 721

Answers (2)

Rafal Zajac
Rafal Zajac

Reputation: 1653

Another way to do the same:

df[[4 in daystuple for daystuple in df[‘days’]]]

Upvotes: 1

Anand S Kumar
Anand S Kumar

Reputation: 90919

One way to do this would be to use Series.apply method, though this may not be very fast. Example -

df[df['days'].apply(lambda x: 4 in x)]

Demo -

In [139]: df
Out[139]:
         start         end                          days
0   2015-07-01  2015-07-07         (1, 2, 3, 4, 5, 6, 7)
1   2015-07-08  2015-07-14    (8, 9, 10, 11, 12, 13, 14)
2   2015-07-15  2015-07-21  (15, 16, 17, 18, 19, 20, 21)
3   2015-07-22  2015-07-28  (22, 23, 24, 25, 26, 27, 28)
4   2015-07-29  2015-08-04      (29, 30, 31, 1, 2, 3, 4)
5   2015-08-05  2015-08-11       (5, 6, 7, 8, 9, 10, 11)
6   2015-08-12  2015-08-18  (12, 13, 14, 15, 16, 17, 18)
7   2015-08-19  2015-08-25  (19, 20, 21, 22, 23, 24, 25)
8   2015-08-26  2015-09-01   (26, 27, 28, 29, 30, 31, 1)
9   2015-09-02  2015-09-08         (2, 3, 4, 5, 6, 7, 8)
10  2015-09-09  2015-09-15   (9, 10, 11, 12, 13, 14, 15)
11  2015-09-16  2015-09-22  (16, 17, 18, 19, 20, 21, 22)
12  2015-09-23  2015-09-29  (23, 24, 25, 26, 27, 28, 29)

In [141]: df['days'][0]
Out[141]: (1, 2, 3, 4, 5, 6, 7)

In [142]: type(df['days'][0])
Out[142]: tuple

In [143]: df[df['days'].apply(lambda x: 4 in x)]
Out[143]:
        start         end                      days
0  2015-07-01  2015-07-07     (1, 2, 3, 4, 5, 6, 7)
4  2015-07-29  2015-08-04  (29, 30, 31, 1, 2, 3, 4)
9  2015-09-02  2015-09-08     (2, 3, 4, 5, 6, 7, 8)

Upvotes: 1

Related Questions