Sep-04-2021, 08:50 PM
Hi there,
I have the following, first part of a Python Pandas Code :-
I have attached the needed BBMF2011.csv File, to this thread.
Any help would be much appreciated.
Best Regards
Eddie Winch
I have the following, first part of a Python Pandas Code :-
import pandas as pd pd.chained_assignment=None data = pd.read_csv(r'C:\Users\Admin\Desktop\BBMF2011.csv') pd.options.display.max_rows = 1000 pd.options.display.max_columns = 1000 df = pd.DataFrame(data, columns= ['Venue','A/C','D/FP/T/P','Date','Bid',"Dur'"]) #df[(df['Venue'].str.contains('[a-zA-Z]') & (df['D/FP/T/P'].str.contains('DISPLAY') & df['A/C'].str.contains("DHS|DAK|HS|SPIT")) & (df['A/C'] != 'LHS') & (df['A/C'] != 'LANC'))] #df["Date"].fillna("No Date", inplace = True) df["Date"].fillna(method='ffill', inplace = True) df['A/C'].unique().tolist() rename_map = { 'DAK': 'D', 'SPIT': 'S', 'LANC': 'L', 'HURRI': 'H', 'PARA': 'P' } df['A/C std'] = df['A/C'].replace(rename_map) print(df['A/C std'].unique().tolist()) print("=======") #selected = df.loc[df['D/FP/T/P'] == 'FLYPAST'] selected = df.loc[df['D/FP/T/P'].isin(['DISPLAY', 'FLYPAST', 'FP', 'D'])] #selected = df.loc[df['A/C'].isin(['DS', 'DH', 'DHS', 'SD', 'HSD'])] df1=df groupby_venue_date = selected.groupby(['Venue', 'Bid', 'Date', 'D/FP/T/P']) aircraft = groupby_venue_date['A/C std'].apply(''.join).rename('Aircraft-Allocation') def sorted_string(s): return ''.join(sorted(s)) def remove_duplicate_chars(s): return ''.join(set(s)) ##################### ##################### ##################### ##################### df=pd.DataFrame(aircraft) #print(df) #original df indexNames = df[~df['Aircraft-Allocation'].str.contains('D',na=False)].index df.drop(indexNames , inplace=True) #print(df)#prints all rows having D and removes remaining rows df['Aircraft-Allocation']=df['Aircraft-Allocation'].replace(to_replace='SD', value='DS') df['Aircraft-Allocation']=df['Aircraft-Allocation'].replace(to_replace=['HSD','HDS','SDH', 'SHSD'], value='DHS') #print(df)#replaces the SD with DS and HSD,HDS and SHSD with DHS df = df.drop(df[df['Aircraft-Allocation'] == 'D'].index)#remove value that match letter D df = df.drop(df[df['Aircraft-Allocation'] == 'DD'].index) df = df.drop(df[df['Aircraft-Allocation'] == 'DL'].index) df = df.drop(df[df['Aircraft-Allocation'] == 'LD'].index) df = df.drop(df[df['Aircraft-Allocation'] == 'LDS'].index) df = df.drop(df[df['Aircraft-Allocation'] == 'LSSD'].index) #df = df.drop(df[df['Aircraft-combined'] != 'D'].index)#if need the rows that match only letter D in selected dataframe column #column names before alignment #print("column names: before change ", list(df.columns.values.tolist())) #print("=======") #print(df) #print("=======") #to fix column name alignment df.to_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv') df = pd.read_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv',names=['Venue','Bid','Date','D/FP/T/P','Aircraft-Allocation'], header=0 ) #column names before alignment #print("column names: after change ",list(df.columns.values.tolist())) #print("=======") #print(df) #print("=======") ###My code Starts here x,y,z=df['Venue'],df['Bid'],df['Date'] x=list(x) y=list(y) z=list(z) import numpy as np h=np.array(y) np.unique(h) yy=[] for jj in y: yy.append(str(jj)) t= df1['Venue'].isin(x) df2=df1[t] df2 g=df2['Date'].isin(z) df3=df2[g] df3=pd.DataFrame(df3) df3 g=df3['Bid'].isin(yy) df4=df3[g] df4=pd.DataFrame(df4) df4 column_names = ['Venue','A/C','D/FP/T/P','Date','Bid',"Dur'",'Aircraft-Allocation'] dff = pd.DataFrame(columns = column_names) dff.to_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv') dff = pd.read_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv',names=['Venue','A/C','D/FP/T/P','Date','Bid',"Dur'",'Aircraft-Allocation'], header=0 ) df4 df4.to_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv') df4 = pd.read_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\tempfiletest7.csv',names=['Venue','A/C','D/FP/T/P','Date','Bid',"Dur'",'Aircraft-Allocation'], header=0 ) for x in range(0,len(df4)): y=x+1 z=x+2 try: if ((((df4.iloc[x][6]))=='DSH') or (((df4.iloc[x][6]))=='SD') or (((df4.iloc[x][6]))=='DS') or (((df4.iloc[x][6]))=='HSD') or (((df4.iloc[x][6]))=='HDS') or (((df4.iloc[x][6]))=='SHSD') or (((df4.iloc[x][6]))=='DHS')): dff3 ={'Venue':df4.iloc[x][0],'A/C':df4.iloc[x][1],'D/FP/T/P':df4.iloc[x][2],'Date':df4.iloc[x][3],'Bid':df4.iloc[x][4],"Dur'":df4.iloc[x][5],'Aircraft-Allocation':df4.iloc[x][6]} dff = dff.append(dff3,ignore_index=True) elif ((df4.iloc[x][0]==df4.iloc[y][0]==df4.iloc[z][0]) and (df4.iloc[x][3]==df4.iloc[y][3]==df4.iloc[z][3]) ): sidhu=df4.iloc[x][6]+df4.iloc[y][6]+df4.iloc[z][6] dff4 ={'Venue':df4.iloc[x][0],'A/C':df4.iloc[x][1],'D/FP/T/P':df4.iloc[x][2],'Date':df4.iloc[x][3],'Bid':df4.iloc[x][4],"Dur'":df4.iloc[x][5],'Aircraft-Allocation':sidhu} dff5 ={'Venue':df4.iloc[y][0],'A/C':df4.iloc[y][1],'D/FP/T/P':df4.iloc[y][2],'Date':df4.iloc[y][3],'Bid':df4.iloc[y][4],"Dur'":df4.iloc[y][5],'Aircraft-Allocation':sidhu} dff6 ={'Venue':df4.iloc[z][0],'A/C':df4.iloc[z][1],'D/FP/T/P':df4.iloc[z][2],'Date':df4.iloc[z][3],'Bid':df4.iloc[z][4],"Dur'":df4.iloc[z][5],'Aircraft-Allocation':sidhu} dff = dff.append(dff4,ignore_index=True) dff = dff.append(dff5,ignore_index=True) dff= dff.append(dff6,ignore_index=True) elif ((df4.iloc[x][3]==df4.iloc[y][3]) and (df4.iloc[x][0]==df4.iloc[y][0])): sidhu=df4.iloc[x][6]+df4.iloc[y][6] dff1 = {'Venue':df4.iloc[x][0],'A/C':df4.iloc[x][1],'D/FP/T/P':df4.iloc[x][2],'Date':df4.iloc[x][3],'Bid':df4.iloc[x][4],"Dur'":df4.iloc[x][5],'Aircraft-Allocation':sidhu} dff2 = {'Venue':df4.iloc[y][0],'A/C':df4.iloc[y][1],'D/FP/T/P':df4.iloc[y][2],'Date':df4.iloc[y][3],'Bid':df4.iloc[y][4],"Dur'":df4.iloc[y][5],'Aircraft-Allocation':sidhu} dff = dff.append(dff1,ignore_index=True) dff = dff.append(dff2,ignore_index=True) else: pass except Exception: pass x=x-1 y=y-1 if((df4.iloc[x][3]==df4.iloc[y][3]) and (df4.iloc[x][0]==df4.iloc[y][0])): sidhu=df4.iloc[x][6]+df4.iloc[y][6] dff7 = {'Venue':df4.iloc[x][0],'A/C':df4.iloc[x][1],'D/FP/T/P':df4.iloc[x][2],'Date':df4.iloc[x][3],'Bid':df4.iloc[x][4],"Dur'":df4.iloc[x][5],'Aircraft-Allocation':sidhu} dff8 = {'Venue':df4.iloc[y][0],'A/C':df4.iloc[y][1],'D/FP/T/P':df4.iloc[y][2],'Date':df4.iloc[y][3],'Bid':df4.iloc[y][4],"Dur'":df4.iloc[y][5],'Aircraft-Allocation':sidhu} dff = dff.append(dff7,ignore_index=True) dff = dff.append(dff8,ignore_index=True) dff['Aircraft-Allocation']=dff['Aircraft-Allocation'].replace(to_replace='SD', value='DS') dff['Aircraft-Allocation']=dff['Aircraft-Allocation'].replace(to_replace='SSD', value='DSS') dff['Aircraft-Allocation']=dff['Aircraft-Allocation'].replace(to_replace=['HSD','HDS','SDH', 'SHSD'], value='DHS') dff['Dakota And Fighters In The Same Slot']='Yes' for x in range(0,len(dff)): y=x+1 try: if ((dff.iloc[x][3]==dff.iloc[y][3]) and (dff.iloc[x][0]==dff.iloc[y][0])): if dff.iloc[x][5]==dff.iloc[y][5]: dff['Dakota And Fighters In The Same Slot'][x]='Yes' dff['Dakota And Fighters In The Same Slot'][y]='Yes' else: dff['Dakota And Fighters In The Same Slot'][x]='No' dff['Dakota And Fighters In The Same Slot'][y]='No' else: continue except Exception: break dff=dff.drop('A/C',axis=1) dff=dff.drop_duplicates(subset=['Venue','Date','Bid','Dakota And Fighters In The Same Slot'], keep='first', inplace=False) dff=dff.drop_duplicates(subset=None, keep='first', inplace=False) #dff.loc[6,"Dur'"]=10 dff = dff.drop(dff[dff['Aircraft-Allocation'] == 'DLHS'].index) dff = dff.drop(dff[dff['Aircraft-Allocation'] == 'DLS'].index) dff = dff.drop(dff[dff['Aircraft-Allocation'] == 'HSL'].index) dff = dff.drop(dff[dff['Aircraft-Allocation'] == 'LHSD'].index) dff["Dur'"]= pd.to_datetime(dff["Dur'"],format='%M').apply(pd.Timestamp) dff["Dur'"] = pd.to_datetime(dff["Dur'"], format='%H:%M:%S').dt.time dff['Date']= pd.to_datetime(dff['Date'],format='%d-%b') dff['Date']= pd.to_datetime(dff['Date']).dt.strftime('%d-%m-2011') dff=dff.fillna('--') dff.to_csv(r'C:\Users\Admin\Desktop\BBMF Schedules And Master Forum Thread Texts\BBMF-2011-Code-2021-Test.csv') df11=dff df11.reset_index(drop=True, inplace=True) df11Which gives the following Error :-
Error:TypeError: sequence item 0: expected str instance, float found
Pointing to the following part of that Code :-30 df1=df 31 groupby_venue_date = selected.groupby(['Venue', 'Bid', 'Date', 'D/FP/T/P']) ---> 32 aircraft = groupby_venue_date['A/C std'].apply(''.join).rename('Aircraft-Allocation') 33 def sorted_string(s): 34 return ''.join(sorted(s))I am unsure, what is causing the Error, and how to fix it ? Could someone tell me, what might be causing the Error, and how I can fix it ?
I have attached the needed BBMF2011.csv File, to this thread.
Any help would be much appreciated.
Best Regards
Eddie Winch
Attached Files