I have a several pandas Data Frames stored in a dictionary:
df1=pd.DataFrame({'product':['ajoijoft','bbhjbh','cser','sesrd','yfgjke','tfyfyf','drdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df2=pd.DataFrame({'product':['ajyughjoijoft','bdrddbhjbh','rdtrdcser','sdtrdthddesrd','yawafgjke','tesrgsfyfyf','sresedrdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df3=pd.DataFrame({'product':['joijoft','bdbhjbh','rdcser','sdhddesrd','wajke','yf','sresedrdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df_dict = {"A":df1,'B':df2, "C":df3}
I want to know the length of the each string in product, so I write as below.
for i, ii in df_dict.items():
ii['Productsize'] = ii['product'].str.len()
This worked and I could get the length for all "product".
Next, I want to remove rows that have a short product string length, that is: Productsize < 6
I tried to use this code:
for i, ii in df_dict.items():
ii=ii[~(ii['Productsize'] <= 6)]
However, this did not work. If I write individually (i.e. not in a loop) as below, it will work though.
df1=df1[~(df1['Productsize'] <= 6)]
Does anyone know what the problem might be?
I tried you guys suggested. Unfortunately, this does not work. Do you know why...? Here is the code.
df1=pd.DataFrame({'product':['ajoijoft','bbhjbh','cser','sesrd','yfgjke','tfyfyf','drdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df2=pd.DataFrame({'product':['ajyughjoijoft','bdrddbhjbh','rdtrdcser','sdtrdthddesrd','yawafgjke','tesrgsfyfyf','sresedrdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df3=pd.DataFrame({'product':['joijoft','bdbhjbh','rdcser','sdhddesrd','wajke','yf','sresedrdrtjg'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
df_dict = {"A":df1,'B':df2, "C":df3}
for i, ii in df_dict.items():
ii['Productsize'] = ii['product'].str.len()
for i, ii in df_dict.items():
df_dict[i] = ii[~(ii['Productsize'] <= 6)]