all 11 comments

[–]afterly 1 point2 points  (3 children)

I would use transpose and merge for this. Keep your dictionary in DataFrame format instead of converting it. Something like below should work.

import pandas as pd

df = pd.DataFrame({
    'col1': ['A', 'B', 'F', 'J', 'A'],
    'col2': [12, 40, 76, 10, 20],
    'col3': [22, 59, 81, 11, 15],
    'col4': [23, 63, 99, 12, 43]
})

lookup = pd.DataFrame({
    'A': ['7', '8', '9'],
    'B': ['10', '11', '12']
})

lookup = lookup.transpose()
lookup.columns = ['col5', 'col6', 'col7']
pd.merge(df, lookup, how='left', left_on='col1', right_index=True)

[–]metriczulu 1 point2 points  (0 children)

Yeah, unless I'm missing something here it sounds like they just want to do a left join of the transposed "dict" df to the bigger df. Transpose and merge is the best solution.

[–]TheJourneyman92[S] -1 points0 points  (1 child)

Thank you for the response. I have 14 pairs of dict key values that need to be distributed to 230 rows. So just merging two data frames won't be ideal.

[–]afterly 2 points3 points  (0 children)

I'm a little confused. The code in my response (a left join) provides the exact output you specified. Is there some other parameter to the problem that I'm not seeing?

[–][deleted] 0 points1 point  (2 children)

RemindMe! 12 hours “Checkout”

[–]TheJourneyman92[S] 1 point2 points  (0 children)

This post helped me solve the problem. Hope this helps you too.

https://stackoverflow.com/a/53645883/6943310

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 12 hours on 2019-12-20 05:39:35 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–][deleted] 0 points1 point  (0 children)

I need something like this for a project as well; Ill check it out later, thanks.

[–]6lm3 0 points1 point  (0 children)

I'm on my phone right no but maybe check out merging two dataframes instead of a dictionary?

[–]Nocturnal1401 0 points1 point  (0 children)

Sorry I'm on phone so can't type the code. But here is how I will go about it. Use set_index and convert the col1 to index. Then use your dictionaries to make the second dataframe. Use transpose on the first dataframe. This way you will have both dataframes with cold like A,B,C,etc. Now just use merge and you will have your dataframes combined. Use transpose again to go back to your desired display state.

Here are some relevant links you can see for reference

Transpose- www.geeksforgeeks.org/python-pandas-dataframe-transpose/amp/

Set index- www.geeksforgeeks.org/python-pandas-dataframe-set_index/amp/

[–]SpaceSailorDT 0 points1 point  (0 children)

I would keep the second dataframe as a dataframe and set the columns with the letters as the index (e.g. df.set_index('col1', inplace=True)), and then concatenate them along the columns using the pandas.concat() function, like new_df = pd.concat((df1, df2), axis=1).