column order changes when copying rows in datafram - 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: column order changes when copying rows in datafram (/thread-15943.html) |
column order changes when copying rows in datafram - swapnaharish - Feb-07-2019 I am copying data rows for a specified criteria from one dataframe to another. However, the column order changes while doing so. please see the code below: import pandas as pd from pandas import DataFrame # Creating the dataframes emp_data = pd.read_excel('employee_data.xlsx') rows = len(emp_data.index) filtered_data = pd.DataFrame() count = 0 count_filter = 0 # Logic for filtering while count < rows: if(emp_data.iloc[count][8] < -20): filtered_data = filtered_data.append(emp_data.loc[count]) count_filter = count_filter + 1 count = count + 1 # Printing filtered data print(filtered_data) # Writing data to excel file import xlsxwriter out_path = "D:\\New folder" writer = pd.ExcelWriter(out_path , engine='xlsxwriter') filtered_data.to_excel(writer, sheet_name='Sheet1') writer.save()So, the original dataframe is emp_data and the new dataframe is filtered_data. Can you please suggest how I can ensure that the column order is maintained? Thanks Larz60+ for the details and fixing the issue. I will follow this in future posts. RE: column order changes when copying rows in datafram - scidam - Feb-07-2019 The power of Pandas is you don't need to write such loops. From the code I concluded that you are trying to select rows from the original dataframe, where values in column 8 are less -20. Using pandas this could be done easily: filtered_data = emp_data.loc[emp_data.iloc[:, 8] < -20] |