1

image is added so that you can look how my dataframe df2 will look like

i have written a code to check condition and if it matches it will update the items of my list but it's working at all it return me the same not updated or same previous list. is this code wrong? please suggest

emp1=[]
for j in range(8,df.shape[0],10):
    for i in range(2,len(df.columns)):
        b=df.iloc[j][3]
        #values are appended from dataframe to list and values are like['3 : 3','4 : 4',.....]

ess=[]
for i in range(df2.shape[0]):
    a=df2.iloc[i][2]
    ess.append(a) #values taken from file which are(3,4,5,6,7,8,....etc i.e unique id number)
nm=[]
for i in range(df2.shape[0]):
    b=df2.iloc[i][3]
    nm.append(b) #this list contains name of the employees

ap= [i.split(' : ', 1)[0] for i in emp1]  #split it with ' : ' and stored in two another list(if 3 : 3 then it will store left 3)
bp= [i.split(' : ', 1)[1] for i in emp1] #if 3 : 3 the it will store right 3
cp=' : '

#the purpose is to replace right 3 with the name i.e 3 : nameabc and then again join to the list   
for i in range(len(emp1)):
    for j in range(len(ess)):
        #print(i,j)
        if ap[i]==ess[j]:
           bp[i]=nm[j]

for i in range(df.shape[0]):
    ap[i]=ap[i]+cp      # adding ' : ' after left integer    
    
emp = [i + j for i, j in zip(ap, bp)] # joining both the values

expected output: if emp1 contains 3 : 3 then after processing it should show like 3 : nameabc

Community
  • 1
  • 1
  • Are you sure this condition: `if ap[i]==df2.iloc[j][2]:` is `True` at some point? – Sayandip Dutta Dec 18 '19 at 06:01
  • Can you post your entire code with expected output and the actual output – saintlyzero Dec 18 '19 at 06:42
  • you should use `print()` to check values in variables - it helps to see problem. `print(i, j, ap[i], df2.iloc[j][2], ap[i]==df2.iloc[j][2] )` – furas Dec 18 '19 at 06:53
  • You should provide us with an example of the data you're working on, otherwise it's impossible to tell exactly how or why your code is not working. – Grismar Dec 18 '19 at 07:00
  • Please replace the image with text. A simple way to produce pretty tabular text to copy-paste is described [here](https://stackoverflow.com/questions/18528533/pretty-printing-a-pandas-dataframe). – norok2 Dec 18 '19 at 07:52

1 Answers1

0

May be I missed something, but I don't see you assigning any value to emp1. Its empty and for "ap" and "bp", you are looping over empty emp1. That may be the one causing problem.

Teja
  • 3
  • 1
  • 3