Home > Blockchain >  Python: loop index issue
Python: loop index issue

Time:11-07

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

CodePudding user response:

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]

CodePudding user response:

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

  • Related