Replace values in data frame with valued of another dataframe.

0 votes

I have the following data set

fruits quality
apple good
mangoes Bad

 

I want the data frame to look like this:
fruits quality
1 2
1 2
2 1

Where in fruits, 1 stand for apple and 2 for mangoes
and in quality, 1 stand for good and 2 for bad.
How do I write a python code for this?
May 30 in Python by Puneeth
12 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hi @Puneeth, you use something like this:

f = pd.DataFrame({'Fruits':['1.apple','2.Mangoes', np.nan],
                   'quality':['1.Good','2.Bad']})
print (df)
    
d={x:df[x].str.extract(r'(\d+)\.(.+)').dropna().set_index(0)[1].to_dict() for x in df.columns}
print (d)
{'Fruits': {'1': 'Apples', '2': 'Mangoes'}, 
 'Ethnicity': {'1': 'Good', '2': 'Bad'}}

df1 = pd.DataFrame({'Fruits':[1,1,2],
                   'Quality':[2,2,1]})
answered May 30 by Ashish

Related Questions In Python

0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,140 points
395 views
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 14,980 points
3,109 views
+1 vote
1 answer

How to replace id with attribute corresponding to id of another table?

Use the following query statement and let ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 56,140 points
16 views
0 votes
1 answer

How to replace id with attribute corresponding to id of another table?

Try looking the given link for better ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 56,140 points
16 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
55 views
+4 votes
6 answers
0 votes
1 answer

How to find the sum of rows in Pandas dataframe?

You can use a combination groupby function with the sum() method. ...READ MORE

answered May 9 in Python by Jisha
27 views
0 votes
1 answer

How to fill Null/NaN values in Pandas Dataframe?

Pandas allows you to change all the ...READ MORE

answered May 13 in Python by Rajat
14 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.