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
4,077 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,020 points
11,722 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,510 points
122,321 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,020 points
2,476 views
0 votes
0 answers

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

Using SQLAlchemy, I can query a bunch ...READ MORE

Oct 3, 2018 in Python by eatcodesleeprepeat
• 4,710 points
706 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,434 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
4,070 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,870 points
44,444 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,608 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