NaN
NaN

Reputation: 691

Pandas dataframe non unique values of one column based on unique values of another column

I have a Pandas dataframe and I want to get a list of all of the unique years for unique events. I don't care about the DIRECTION column, I just want a list of DATE's. I don't necessarily want the DATE's to be unique, because there are sometimes multiple ID's for the same date, but I don't need all of the DIRECTION's for the same date.

Pandas df

ID DIRECTION DATE
ABA Z 2019
ABA N 2019
ABA E 2019
ABB Z 2019
ABB N 2019
ABB E 2019
ABC Z 2020
ABC N 2020
ABC E 2020

Expected Output

[2019, 2019, 2020]

Actual Output

[2019, 2020]

Current Code

ids=df['ID'].unique().tolist()
dates=df['DATE'].unique().tolist()
labels, counts = np.unique(dates, return_counts=True)

**

len(counts) == 2
#I want len(counts) == 3

Upvotes: 1

Views: 74

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You want the unique date per id, and then concatenate them into one array:

np.concatenate(df.groupby('ID')['DATE'].unique().values)

Output:

array([2019, 2019, 2020])

Upvotes: 1

Related Questions