![]() |
Reindex columns - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Reindex columns (/thread-32641.html) Pages:
1
2
|
Reindex columns - rr1997 - Feb-23-2021 Hi, How can I reindex columns? I get an error that says "ValueError: Length mismatch: Expected axis has 18 elements, new values have 5 elements" The excel file is attached. import pandas as pd import getpass import sys import datetime from ftplib import FTP_TLS import sys user_id = getpass.getuser() # Read Download and Dealers data = pd.read_csv('C:/Users/'+ user_id +'/Desktop/Portable Script/Download_Report_Teleservices.csv',sep=";") dealers = pd.read_csv('C:/Users/'+ user_id +'/Desktop/Portable Script/Teleservices_Support.csv',engine='python',sep=";") data.head() data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em']) data.columns = ['Local_ID','ID bilhete','Chassis','Entrada','Escalado'] # Add Telefone data["Telefone"]=1 # Add current date column data['Data Envio'] = pd.to_datetime('today').strftime("%d/%m/%Y") data dealers # Merge, remove extra columns, data_merged = data.merge(dealers,how='left', left_on='Local_ID',right_on='Código') data_merged data_merged = data_merged.loc[:,["Telefone","Local","ID bilhete","Chassis","Entrada","Escalado", "Data Envio"]] data_merged.columns = ['Telefone', 'Instalações', 'ID bilhete', 'Chassis', 'Entrada', 'Escalado', 'Data Envio'] # create today name now = datetime.datetime.now() Nday = str(now.day).zfill(2) Nmonth = str(now.month).zfill(2) Nyear = now.year name = "Teleservices" + "_" + str(Nyear)+ "_" +str(Nmonth)+ "_" +str(Nday) # Save file data_merged.to_csv('C:/Users/'+ user_id +'/Desktop/Portable Script/Teleservices_V2/' + name + '.csv',sep=";",index=False,encoding='utf-8-sig') #data_merged.to_csv('C:/Users/rfcr/Rigor/Caetano Retail - Organização/1 - Baviera/5 - Power BI/Teleservices/Daily Tickets/' + name + '.csv',sep=";",index=False,encoding='utf-8-sig') #Login to FTP (TLS secure) ftp = FTP_TLS() ftp.set_debuglevel(2) ftp.connect('ftp.gruposalvadorcaetano.pt', port=21) ftp.login(user='cretail.csi', passwd='cSvJCasuo2kzTH') # Open File file = open('C:/Users/'+ user_id +'/Desktop/Portable Script/Teleservices_V2/' + name + '.csv','rb') # Change directory in FTP ftp.cwd('/CCENTER/IN') # Upload file to FTP with correct name ftp.storbinary('STOR ' + name + '.csv', file) # Close FTP and File file.close() ftp.close() # Finsih Script print("Finished") sys.exit() RE: Reindex columns - eddywinch82 - Feb-23-2021 Hi rr1997, I don't think you have uploaded the File, could you do it again ? And I think I will be able to help you. Regards Eddie Winch RE: Reindex columns - rr1997 - Feb-23-2021 (Feb-23-2021, 03:22 PM)eddywinch82 Wrote: Hi rr1997, Hi, I can't attach files, so this url have my file, can you please check that? https://filebin.net/wzqr9zvflgi3nmxb RE: Reindex columns - eddywinch82 - Feb-23-2021 Hi rr1997, Many thanks for posting the Url for the File, I will get back to you soon. Regards Eddie Winch RE: Reindex columns - eddywinch82 - Feb-23-2021 Hi rr1997, If I change the following line Of Code, which you have provided. From :- data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em'])To :- data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em'], axis='columns')I now get the Error :- Could you also upload, your other .csv File.Teleservices_Support.csv ? As that is also used in the Code you provide, and I think it's absence, may be causing that KeyError. If you have that File, The Code you provide may work now, with the change I made. But I would like to try the Code out myself, with the other .csv File if that is okay with you ? Eddie Winch RE: Reindex columns - rr1997 - Feb-24-2021 (Feb-23-2021, 08:11 PM)eddywinch82 Wrote: Hi rr1997, Hi, sorry for the waiting time here it is the url: https://filebin.net/643nkv5flvy2ceb8 RE: Reindex columns - eddywinch82 - Feb-24-2021 Hi rr1997, Many thanks for uploading, the other .csv File for me. When I run the Code again, with both .csv Files present on my computer, I now get an Output .csv File, do you now get the Output, you wanted to get ? I.e. the Output .csv File that I get, when you also run the Code ? Best Regards Eddie Winch ![]() RE: Reindex columns - rr1997 - Feb-24-2021 (Feb-24-2021, 03:31 PM)eddywinch82 Wrote: Hi rr1997, When I run the code I can't get that output .csv and I want to get that .csv RE: Reindex columns - eddywinch82 - Feb-24-2021 Hi rr1997, This is the Code I am using :- import pandas as pd import getpass import sys import datetime from ftplib import FTP_TLS import sys user_id = getpass.getuser() # Read Download and Dealers data = pd.read_csv(r'C:/Users/'+ user_id +'/Desktop/Download_Report_Teleservices.csv',sep=";") dealers = pd.read_csv(r'C:/Users/'+ user_id +'/Desktop/Teleservices_Support.csv',engine='python',sep=";") data.head() data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em'], axis='columns') data.columns = ['Local_ID','ID bilhete','Chassis','Entrada','Escalado'] # Add Telefone data["Telefone"]=1 # Add current date column data['Data Envio'] = pd.to_datetime('today').strftime("%d/%m/%Y") data dealers # Merge, remove extra columns, data_merged = data.merge(dealers,how='left', left_on='Local_ID',right_on='Código') data_merged data_merged = data_merged.loc[:,["Telefone","Local","ID bilhete","Chassis","Entrada","Escalado", "Data Envio"]] data_merged.columns = ['Telefone', 'Instalações', 'ID bilhete', 'Chassis', 'Entrada', 'Escalado', 'Data Envio'] # create today name now = datetime.datetime.now() Nday = str(now.day).zfill(2) Nmonth = str(now.month).zfill(2) Nyear = now.year name = "Teleservices" + "_" + str(Nyear)+ "_" +str(Nmonth)+ "_" +str(Nday) # Save file data_merged.to_csv('C:/Users/'+ user_id +'/Desktop/' + name + '.csv',sep=";",index=False,encoding='utf-8-sig') #data_merged.to_csv('C:/Users/rfcr/Rigor/Caetano Retail - Organização/1 - Baviera/5 - Power BI/Teleservices/Daily Tickets/' + name + '.csv',sep=";",index=False,encoding='utf-8-sig') #Login to FTP (TLS secure) ftp = FTP_TLS() ftp.set_debuglevel(2) ftp.connect('ftp.gruposalvadorcaetano.pt', port=21) ftp.login(user='cretail.csi', passwd='cSvJCasuo2kzTH') # Open File file = open('C:/Users/'+ user_id +'/Desktop/' + name + '.csv','rb') # Change directory in FTP ftp.cwd('/CCENTER/IN') # Upload file to FTP with correct name ftp.storbinary('STOR ' + name + '.csv', file) # Close FTP and File file.close() ftp.close() # Finsih Script print("Finished") sys.exit()I have both .csv Files on my Desktop, and have the Output .csv File saved to the desktop too. Try the above Code, you may not be getting the Output .csv File I get, due to an incorrect File Path(s) in the Code you are using, try putting both .csv Files on your Desktop like me. Before trying the above suggestions, try changing the following two lines of Code. From :- # Read Download and Dealers data = pd.read_csv('C:/Users/'+ user_id +'/Desktop/Portable Script/Download_Report_Teleservices.csv',sep=";") dealers = pd.read_csv('C:/Users/'+ user_id +'/Desktop/Portable Script/Teleservices_Support.csv',engine='python',sep=";")To :- # Read Download and Dealers data = pd.read_csv(r'C:/Users/'+ user_id +'/Desktop/Portable Script/Download_Report_Teleservices.csv',sep=";") dealers = pd.read_csv(r'C:/Users/'+ user_id +'/Desktop/Portable Script/Teleservices_Support.csv',engine='python',sep=";")And changing this line of Code. From :- data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em'])To :- data = data.reindex(['Número do ponto de serviço autorizado','ID','VIN','Data de criação','Escalado em'], axis='columns')In the Code you provide, at the start of this Thread. Best Regards Eddie Winch RE: Reindex columns - rr1997 - Feb-25-2021 (Feb-24-2021, 07:18 PM)eddywinch82 Wrote: Hi rr1997, Thank you so much!! |