0

Idea: I want to replace parts of data1 with newer price data.

Q1: Why are 'laptop' and 'printer' updated, but not 'chair'?

Q2: 'bed' in data2 does not exist for data1. In that case, there can't be an update to data1. I'm just wondering why there is no error like "'bed' has no match in data1"?

import pandas as pd

data1 =pd.DataFrame({'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
        'price': [0, 4, 6, 7, 9]
        })
data2 =pd.DataFrame({'product_name': ['laptop', 'printer','chair','bed'],
        'price': [89,32,34,355]
        })

indi = data2['product_name']

for i in indi:
    temp = data2.loc[data2['product_name'] == '%s'%i,'price']
    data1.loc[data1['product_name'] == '%s'%i,'price'] = temp

2 Answers 2

1

You need to do this: (temp.iloc[0])

for i in indi:
    temp = data2.loc[data2['product_name'] == '%s'%i,'price']
    data1.loc[data1['product_name'] == '%s'%i,'price'] = temp.iloc[0]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! I'd like to understand why I have to add ".iloc[0]. I thought temp was a "number".
Glad it helped you! If you type print(temp), you will see that it is not really a number. It has name and index attached to it.
Please upvote if this answer was useful.
Thanks! I just wonder why updating works for the first two entries, but then fails for the last one (when there are some entries in data 2 that are not in data1).
1

I believe this may have something to with non-matching indexes for the chair entries. When you loop through indi, you get not only the product_name from data but also its corresponding index. Here's a better way to deal with situations like this:

In [53]: data1
Out[53]:
  product_name  price
0       laptop      0
1      printer      4
2       tablet      6
3         desk      7
4        chair      9

In [54]: data2
Out[54]:
  product_name  price
0       laptop     89
1      printer     32
2        chair     34
3          bed    355

In [55]: for row in data2.itertuples():
    ...:     data1.loc[data1['product_name']==row.product_name, 'price'] = row.price
    ...:

In [56]: data1
Out[56]:
  product_name  price
0       laptop     89
1      printer     32
2       tablet      6
3         desk      7
4        chair     34

In [57]: data2
Out[57]:
  product_name  price
0       laptop     89
1      printer     32
2        chair     34
3          bed    355

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.