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
39 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 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,520 points
911 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
• 15,350 points
4,877 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,520 points
26 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,520 points
22 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 174 views
+4 votes
6 answers
0 votes
1 answer

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

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

answered Jul 8 in Python by SDeb
• 13,160 points
241 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 in Python by Jisha
81 views