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, 2019 in Python by Puneeth
3,806 views

1 answer to this question.

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, 2019 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
• 58,090 points
11,193 views
+2 votes
4 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
• 15,520 points
119,756 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
• 58,090 points
2,062 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
• 58,090 points
447 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,070 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,491 views
0 votes
3 answers

How to replace negative numbers in Pandas Data Frame by zero?

 If all your columns are numeric, you ...READ MORE

answered Dec 16, 2020 in Python by Rajiv
• 8,910 points
43,132 views
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, 2019 in Python by Jisha
4,293 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP