Feb-07-2019, 10:44 AM
(This post was last modified: Feb-07-2019, 11:20 AM by swapnaharish.)
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:
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.
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.