Get latest value looked up from other dataframe - python
My first data frame
product=pd.DataFrame({
'Product_ID':[101,102,103,104,105,106,107,101],
'Product_name':['Watch','Bag','Shoes','Smartphone','Books','Oil','Laptop','New Watch'],
'Category':['Fashion','Fashion','Fashion','Electronics','Study','Grocery','Electronics','Electronics'],
'Price':[299.0,1350.50,2999.0,14999.0,145.0,110.0,79999.0,9898.0],
'Seller_City':['Delhi','Mumbai','Chennai','Kolkata','Delhi','Chennai','Bengalore','New York']
})
My 2nd data frame has transactions
customer=pd.DataFrame({
'id':[1,2,3,4,5,6,7,8,9],
'name':['Olivia','Aditya','Cory','Isabell','Dominic','Tyler','Samuel','Daniel','Jeremy'],
'age':[20,25,15,10,30,65,35,18,23],
'Product_ID':[101,0,106,0,103,104,0,0,107],
'Purchased_Product':['Watch','NA','Oil','NA','Shoes','Smartphone','NA','NA','Laptop'],
'City':['Mumbai','Delhi','Bangalore','Chennai','Chennai','Delhi','Kolkata','Delhi','Mumbai']
})
I want Price from 1st data frame to come in the merged dataframe. Common element being 'Product_ID'. Note that against product_ID 101, there are 2 prices - 299.00 and 9898.00. I want the later one to come in the merged data set i.e. 9898.0 (Since this is latest price)
Currently my code is not giving the right answer. It is giving both
customerpur = pd.merge(customer,product[['Price','Product_ID']], on="Product_ID", how = "left")
customerpur
id name age Product_ID Purchased_Product City Price
0 1 Olivia 20 101 Watch Mumbai 299.0
1 1 Olivia 20 101 Watch Mumbai 9898.0
There is no explicit timestamp so I assume the index is the order of the dataframe. You can drop duplicates at the end:
customerpur.drop_duplicates(subset = ['id'], keep = 'last')
result:
id name age Product_ID Purchased_Product City Price
1 1 Olivia 20 101 Watch Mumbai 9898.0
2 2 Aditya 25 0 NA Delhi NaN
3 3 Cory 15 106 Oil Bangalore 110.0
4 4 Isabell 10 0 NA Chennai NaN
5 5 Dominic 30 103 Shoes Chennai 2999.0
6 6 Tyler 65 104 Smartphone Delhi 14999.0
7 7 Samuel 35 0 NA Kolkata NaN
8 8 Daniel 18 0 NA Delhi NaN
9 9 Jeremy 23 107 Laptop Mumbai 79999.0
Please note keep = 'last' argument since we are keeping only last price registered.
Deduplication should be done before merging if Yuo care about performace or dataset is huge:
product = product.drop_duplicates(subset = ['Product_ID'], keep = 'last')
In your data frame there is no indicator of latest entry, so you might need to first remove the the first entry for id 101 from product dataframe as follows:
result_product = product.drop_duplicates(subset=['Product_ID'], keep='last')
It will keep the last entry based on Product_ID and you can do the merge as:
pd.merge(result_product, customer, on='Product_ID')
Related
Python pandas merge map with multiple values xlookup
I have a dataframe of actor names: df1 actor_id actor_name 1 Brad Pitt 2 Nicole Kidman 3 Matthew Goode 4 Uma Thurman 5 Ethan Hawke And another dataframe of movies that the actors were in: df2 actor_id actor_movie movie_revenue_m 1 Once Upon a Time in Hollywood 150 2 The Others 50 2 Moulin Rouge 200 3 Stoker 75 4 Kill Bill 125 5 Gattaca 85 I want to merge the two dataframes together to show the actors with their movie names and movie revenues, so I use the merge function: df3 = df1.merge(df2, on = 'actor_id', how = 'left') df3 actor_id actor_name actor_movie movie_revenue 1 Brad Pitt Once Upon a Time in Hollywood 150 2 Nicole Kidman Moulin Rouge 50 2 Nicole Kidman The Others 200 3 Matthew Goode Stoker 75 4 Uma Thurman Kill Bill 125 5 Ethan Hawke Gattaca 85 But this pulls in all movies, so Nicole Kidman gets duplicated, and I only want to show one movie per actor. How can I merge the dataframes without "duplicating" my list of actors? How would I merge the movie title that is alphabetically first? How would I merge the movie title with the highest revenue? Thank you!
One way is to continue with the merge and then filter the result set movie title that is alphabetically first # sort by name, movie and then pick the first while grouping by actor df.sort_values(['actor_name','actor_movie'] ).groupby('actor_id', as_index=False).first() actor_id actor_name actor_movie movie_revenue 0 1 Brad Pitt Once Upon a Time in Hollywood 150 1 2 Nicole Kidman Moulin Rouge 50 2 3 Matthew Goode Stoker 75 3 4 Uma Thurman Kill Bill 125 4 5 Ethan Hawke Gattaca 85 movie title with the highest revenue # sort by name, and review (descending), groupby actor and pick first df.sort_values(['actor_name','movie_revenue'], ascending=[1,0] ).groupby('actor_id', as_index=False).first() actor_id actor_name actor_movie movie_revenue 0 1 Brad Pitt Once Upon a Time in Hollywood 150 1 2 Nicole Kidman The Others 200 2 3 Matthew Goode Stoker 75 3 4 Uma Thurman Kill Bill 125 4 5 Ethan Hawke Gattaca 85
I want to create a new column territory based on the city column
Data Frame : city Temperature 0 Chandigarh 15 1 Delhi 22 2 Kanpur 20 3 Chennai 26 4 Manali -2 0 Bengalaru 24 1 Coimbatore 35 2 Srirangam 36 3 Pondicherry 39 I need to create another column in data frame, which contains a boolean value for each city to indicate whether it's a union territory or not. Chandigarh, Pondicherry and Delhi are only 3 union territories here. I have written below code import numpy as np conditions = [df3['city'] == 'Chandigarh',df3['city'] == 'Pondicherry',df3['city'] == 'Delhi'] values =[1,1,1] df3['territory'] = np.select(conditions, values) Is there any easier or efficient way that I can write?
You can use isin: union_terrs = ["Chandigarh", "Pondicherry", "Delhi"] df3["territory"] = df3["city"].isin(union_terrs).astype(int) which checks each entry in city column and if it is in union_terrs, gives True and otherwise False. The astype makes True/False to 1/0 conversion, to get city Temperature territory 0 Chandigarh 15 1 1 Delhi 22 1 2 Kanpur 20 0 3 Chennai 26 0 4 Manali -2 0 0 Bengalaru 24 0 1 Coimbatore 35 0 2 Srirangam 36 0 3 Pondicherry 39 1
Set multiple columns to zero based on a value in another column [duplicate]
This question already has answers here: Change one value based on another value in pandas (7 answers) Closed 2 years ago. I have a sample dataset here. In real case, it has a train and test dataset. Both of them have around 300 columns and 800 rows. I want to filter out all those rows based on a certain value in one column and then set all values in that row from column 3 e.g. to column 50 to zero. How can I do it? Sample dataset: import pandas as pd data = {'Name':['Jai', 'Princi', 'Gaurav','Princi','Anuj','Nancy'], 'Age':[27, 24, 22, 32,66,43], 'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Katauj', 'vbinauj'], 'Payment':[15,20,40,50,3,23], 'Qualification':['Msc', 'MA', 'MCA', 'Phd','MA','MS']} df = pd.DataFrame(data) df Here is the output of sample dataset: Name Age Address Payment Qualification 0 Jai 27 Delhi 15 Msc 1 Princi 24 Kanpur 20 MA 2 Gaurav 22 Allahabad 40 MCA 3 Princi 32 Kannauj 50 Phd 4 Anuj 66 Katauj 3 MA 5 Nancy 43 vbinauj 23 MS As you can see, in the first column, there values =="Princi", So if I find rows that Name column value =="Princi", then I want to set column "Address" and "Payment" in those rows to zero. Here is the expected output: Name Age Address Payment Qualification 0 Jai 27 Delhi 15 Msc 1 Princi 24 0 0 MA #this row 2 Gaurav 22 Allahabad 40 MCA 3 Princi 32 0 0 Phd #this row 4 Anuj 66 Katauj 3 MA 5 Nancy 43 vbinauj 23 MS In my real dataset, I tried: train.loc[:, 'got':'tod']# get those columns # I could select all those columns and train.loc[df['column_wanted'] == "that value"] # I got all those rows But how can I combine them? Thanks for your help!
Use the loc accessor; df.loc[boolean selection, columns] df.loc[df['Name'].eq('Princi'),'Address':'Payment']=0 Name Age Address Payment Qualification 0 Jai 27 Delhi 15 Msc 1 Princi 24 0 0 MA 2 Gaurav 22 Allahabad 40 MCA 3 Princi 32 0 0 Phd 4 Anuj 66 Katauj 3 MA 5 Nancy 43 vbinauj 23 MS
How to perform groupby and mean on categorical columns in Pandas
I'm working on a dataset called gradedata.csv in Python Pandas where I've created a new binned column called 'Status' as 'Pass' if grade > 70 and 'Fail' if grade <= 70. Here is the listing of first five rows of the dataset: fname lname gender age exercise hours grade \ 0 Marcia Pugh female 17 3 10 82.4 1 Kadeem Morrison male 18 4 4 78.2 2 Nash Powell male 18 5 9 79.3 3 Noelani Wagner female 14 2 7 83.2 4 Noelani Cherry female 18 4 15 87.4 address status 0 9253 Richardson Road, Matawan, NJ 07747 Pass 1 33 Spring Dr., Taunton, MA 02780 Pass 2 41 Hill Avenue, Mentor, OH 44060 Pass 3 8839 Marshall St., Miami, FL 33125 Pass 4 8304 Charles Rd., Lewis Center, OH 43035 Pass Now, how do i compute the mean hours of exercise of female students with a 'status' of passing...? I've used the below code, but it isn't working. print(df.groupby('gender', 'status')['exercise'].mean()) I'm new to Pandas. Anyone please help me in solving this.
You are very close. Note that your groupby key must be one of mapping, function, label, or list of labels. In this case, you want a list of labels. For example: res = df.groupby(['gender', 'status'])['exercise'].mean() You can then extract your desired result via pd.Series.get: query = res.get(('female', 'Pass'))
Merge two dataframes based on a column
I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1 place name qty unit NY Tom 2 10 TK Ron 3 15 Lon Don 5 90 Hk Sam 4 49 df2 place name price PH Tom 7 TK Ron 5 Result: df3 place name qty unit NY Tom 2 10 TK Ron 3 15
Option 1 Using df.isin: In [1362]: df1[df1.name.isin(df2.name)] Out[1362]: place name qty unit 0 NY Tom 2 10 1 TK Ron 3 15 Option 2 Performing an inner-join with df.merge: In [1365]: df1.merge(df2.name.to_frame()) Out[1365]: place name qty unit 0 NY Tom 2 10 1 TK Ron 3 15 Option 3 Using df.eq: In [1374]: df1[df1.name.eq(df2.name)] Out[1374]: place name qty unit 0 NY Tom 2 10 1 TK Ron 3 15
You want something called an inner join. df1.merge(df2,on = 'name') place_x name qty unit place_y price NY Tom 2 10 PH 7 TK Ron 3 15 TK 5 The _xand _y happens when you have a column in both data frames being merged.