How do I translate this into python code? (excel vlookup) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: How do I translate this into python code? (excel vlookup) (/thread-27099.html) |
How do I translate this into python code? (excel vlookup) - eeps24 - May-26-2020 I have two dataframes and trying to do a excel vlookup type function. excel's vlookup parameters: vlookup(lookup_value,table_array,col_index_num,range_lookup How would I translate the following into python code? Trying to do vlookup from df1 to df2. I dont know if I use a merge (all column names are different), map, etc. Im stuck on this problem. Im sure its a easy solution. lookup_value: df1 column C apples table_array: df2 A though D col_index: df2 column D (apple_types) below are column names... df1 A | B | C | D ----------------------------------- grapes|berry | apples | mango df2 A | B | C | D ----------------------------------- veggies|meat | drinks | apple_types Thank you RE: How do I translate this into python code? (excel vlookup) - Larz60+ - May-26-2020 Please:
RE: How do I translate this into python code? (excel vlookup) - eeps24 - May-26-2020 Hi Larz, Thank you for responding. I 100% agree an effort should be made. The only problem is I dont know which function to use to get started. (something that works with two dataframes) I dont need the exact code to solve this, just which function should I use to help me get going in the right direction. Example, if someone can say "use *blank* function to solve for this", this would certainly help me to get going on my resolution. From there I can start to develop my own code. Thank you RE: How do I translate this into python code? (excel vlookup) - buran - May-26-2020 pandas.DataFrame.merge()
RE: How do I translate this into python code? (excel vlookup) - eeps24 - May-26-2020 (May-26-2020, 03:19 PM)buran Wrote: thank you! I will begin coding. RE: How do I translate this into python code? (excel vlookup) - eeps24 - May-27-2020 I figured how to use the merge command (sorta). The following code combines both df1 and df2 together- (I added left/right on because this is the column I need to merge on). Works great but I dont want to merge all columns from both dataframes. pd.merge(df1, df2, left_on='apples', right_on='drinks'). I read online and looked at alot of youtube videos and people do associate vlookup with merge. But in all examples they end up bringing in all columns, which I do not want to happen. Ultimately, yes I do want to merge on column C from df1 to df2.."but" I want to keep df1 intact but ONLY bring in column D from df2 into df1. I do not want to bring in all columns from df2. Please look below, this is exactly what I want to happen. CURRENT STATE df1 A | B | C | D ----------------------------------- grapes|berry | apples | mango df2 A | B | C | D ----------------------------------- veggies|meat | drinks | apple_types FUTURE STATE df1 A | B | C | D | E | ----------------------------------- grapes|berry | apples | mango |apple_types Thank you for your help. I am going crazy on this. RE: How do I translate this into python code? (excel vlookup) - eeps24 - May-29-2020 hello, just wondering if someone can please help me with this. thank you |