Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas DataFrame Code Query
#7
Hi nilamo,

Here I have posted the Lines of Code, where I think the issue arises :-

for i,row in temp_df.iterrows():
    text = row[0]
    date = row[1]
    month = row[2]
    if 'Lancaster' in text:
        temp_df.iat[i,3]='L'
    else:
        temp_df.iat[i,3]=None
    if 'Spitfire' in text:
        temp_df.iat[i,4]='S'

    elif 'S x 2' in text:
        temp_df.iat[i,4]='SS'


    elif 'S x 4' in text:
        temp_df.iat[i,4]='SSSS'
    else:
        temp_df.iat[i,4]=None
    if 'Hurricane' in text:
        temp_df.iat[i,5]='H'

    elif 'H x 2' in text:
        temp_df.iat[i,5]='HH'
    else:
        temp_df.iat[i,5]=None
    if 'Dakota' in text:
        temp_df.iat[i,6]='D'
    else:
        temp_df.iat[i,6]=None

    if '16' in date and 'Jun' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='S'
        temp_df.iat[i,5]='H'
        
    elif '17' in date and 'Jun' in month:
        temp_df.iat[i,3]='--'
        temp_df.iat[i,4]='S'
        temp_df.iat[i,5]='H'
    
    if '13' in date and '15' in date and 'Royal' in text and 'Jul' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='SS'
        temp_df.iat[i,5]='--'
        temp_df.iat[i,6]='D'
        
    elif '14' in date and 'Royal' in text and 'Jul' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='SSS'
        temp_df.iat[i,5]='HH'
        temp_df.iat[i,6]='D'

    if '13' in date and 'Sep' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='S'
        temp_df.iat[i,5]='H'
        
for i,row in temp_df.iterrows():
    text = row[1]
    text=text.replace('th','')
    text=text.replace('st','')
    text=text.replace('nd','')
    text=text.replace('rd','')
    temp_df.iat[i,1] = text
    

for i,row in temp_df.iterrows():
    date = row[1]
    month = row[2]
    location = row[0]
    if '-' in date:
        if month == 'May' and 'June' not in date:
            date_list = date.split("-")
            for j in range(int(date_list[0]),int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'May' and 'June' in date:
            date=date.replace(" June","")
            date_list = date.split("-")
            for j in range(int(date_list[0]),32):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            for j in range(1,int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":'June',"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        
        if month == 'June' and 'July' not in date and 'Lancaster - SAT ONLY' not in row[0]:
            date_list = date.split("-")
            for j in range(int(date_list[0]),int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'June' and 'July' not in date and 'Lancaster - SAT ONLY' in row[0]:
            date_list = date.split("-")
            dic = {"LOCATION":row[0],"DATE":str(date_list[0]),"MONTH":row[2],"LANCASTER":'L',"SPITFIRE":'S','HURRICANE':'H','DAKOTA':'--'}
            temp_df=temp_df.append(dic, ignore_index = True)
            dic = {"LOCATION":row[0],"DATE":str(date_list[1]),"MONTH":row[2],"LANCASTER":'--',"SPITFIRE":'S','HURRICANE':'H','DAKOTA':'--'}
            temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'June' and 'July' in date:
            date=date.replace(" July","")
            date_list = date.split("-")
            for j in range(int(date_list[0]),31):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            for j in range(1,int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":'July',"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)

        if month == 'July' and 'August' not in date:
            date_list = date.split("-")
            for j in range(int(date_list[0]),int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'July' and 'August' in date:
            date=date.replace(" August","")
            date_list = date.split("-")
            for j in range(int(date_list[0]),32):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            for j in range(1,int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":'August',"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
            
            
        if month == 'August' and 'September' not in date:
            date_list = date.split("-")
            for j in range(int(date_list[0]),int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'August' and 'September' in date:
            date=date.replace(" September","")
            date_list = date.split("-")
            for j in range(int(date_list[0]),32):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            for j in range(1,int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":'September',"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)

        if month == 'September' and 'October' not in date:
            date_list = date.split("-")
            for j in range(int(date_list[0]),int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
        if month == 'September' and 'October' in date:
            date=date.replace(" October","")
            date_list = date.split("-")
            for j in range(int(date_list[0]),31):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":row[2],"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            for j in range(1,int(date_list[1])+1):
                dic = {"LOCATION":row[0],"DATE":str(j),"MONTH":'October',"LANCASTER":row[3],"SPITFIRE":row[4],'HURRICANE':row[5],'DAKOTA':row[6]}
                temp_df=temp_df.append(dic, ignore_index = True)
            temp_df=temp_df.drop(temp_df[ temp_df['DATE'] == row[1] ].index)
            

temp_df.reset_index(drop=True, inplace=True)

for i,row in temp_df.iterrows():
    date = row[1]
    date=date.replace(' (30 Reserve)',"")
    temp_df.iat[i,1]=date
    

for i,row in temp_df.iterrows():
    month = row[2]
    day = row[1]
    if month == 'May':
        date = '2018-'+str(5)+"-"+str(day)
    elif month == 'June':
        date = '2018-'+str(6)+"-"+str(day)
    elif month == 'July':
        date = '2018-'+str(7)+"-"+str(day)
        
    elif month == 'August':
        date = '2018-'+str(8)+"-"+str(day)
    elif month == 'September':
        date = '2018-'+str(9)+"-"+str(day)
    temp_df.iat[i,1] = date
temp_df = temp_df.drop('MONTH',axis=1)

for i,row in temp_df.iterrows():
    loc = row[0]
    if 'All available BBMF Aircraft' in loc:
        temp_df.iat[i,2] = 'L'
        temp_df.iat[i,3] = 'SSSSS'
        temp_df.iat[i,4] = 'HH'
        temp_df.iat[i,5] = 'D'
Interestingly, with the following two lines of Code :-

if '13' in date and '15' in date and 'Royal' in text and 'Jul' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='SS'
        temp_df.iat[i,5]='--'
        temp_df.iat[i,6]='D'
        
    elif '14' in date and 'Royal' in text and 'Jul' in month:
        temp_df.iat[i,3]='L'
        temp_df.iat[i,4]='SSS'
        temp_df.iat[i,5]='HH'
        temp_df.iat[i,6]='D'
For the 14th July Date, the same Aircraft are assigned, as for the 13th and 15th Date 'Royal' Rows,
in the DataFrame output ?

Regards

Eddie Winch
Reply


Messages In This Thread
Pandas DataFrame Code Query - by eddywinch82 - Feb-05-2021, 03:04 PM
RE: Pandas DataFrame Code Query - by eddywinch82 - Feb-05-2021, 05:07 PM
RE: Pandas DataFrame Code Query - by nilamo - Feb-05-2021, 08:25 PM
RE: Pandas DataFrame Code Query - by eddywinch82 - Feb-08-2021, 06:54 PM
RE: Pandas DataFrame Code Query - by eddywinch82 - Feb-11-2021, 07:12 PM
RE: Pandas DataFrame Code Query - by nilamo - Feb-11-2021, 09:15 PM
RE: Pandas DataFrame Code Query - by eddywinch82 - Feb-12-2021, 09:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 734 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 833 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,642 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  help how to get size of pandas dataframe into MB\GB mg24 1 2,372 Jan-28-2023, 01:23 PM
Last Post: snippsat
  pandas dataframe into csv .... exponent issue mg24 10 1,793 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 836 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,756 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 1,859 May-09-2022, 01:35 PM
Last Post: snippsat
  Regex Expression With Code Query In Pandas eddywinch82 8 2,338 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,317 Jan-21-2022, 06:24 PM
Last Post: mcva

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020