Python Forum

Full Version: column order changes when copying rows in datafram
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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]