ITN00bs
ITN00bs

Reputation: 55

Pandas - merge two lists

I've been searching everywhere for a tip, however can't seem to find an answer. I am trying to show items which have the same type

i.e. here's my dataset

enter image description here

What I want to end up with is a list of "Names" which are both a book and a movie. i.e. the output should be "Harry Potter" and "LoTR".

i.e. a list like below with the "Name" column only which would show the two items:

enter image description here

I was thinking of doing a pivot, but not sure where to go from there.

Upvotes: 0

Views: 439

Answers (3)

Corralien
Corralien

Reputation: 120409

You can use intersection of set:

>>> list(set(df.loc[df['Type'] == 'Movie', 'Name']) \
               .intersection(df.loc[df['Type'] == 'Book', 'Name']))
['Harry Potter', 'LoTR']

Or

>>> df.loc[df['Type'] == 'Movie', 'Name'] \
      .loc[lambda x: x.isin(df.loc[df['Type'] == 'Book', 'Name'])].tolist()
['Harry Potter', 'LoTR']

Upvotes: 0

Riley
Riley

Reputation: 2261

ct = pd.crosstab(df["name"], df["type"]).astype(bool)
result = ct.index[ct["book"] & ct["movie"]].to_list()

Upvotes: 1

prahasanam_boi
prahasanam_boi

Reputation: 896

please try this:

df_new = df[['Name','Type']].value_counts().reset_index()['Name'].value_counts().reset_index()
names = list(df_new[df_new['Name']>1]['index'].unique())

The above code gives all names with more than one type. If you want exactly names with two types, change the 2nd line to this:

names = list(df_new[df_new['Name']==2]['index'].unique())

Upvotes: 0

Related Questions