Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 column order changes when copying rows in datafram
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

# 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')
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.
Larz60+ wrote Feb-07-2019, 11:10 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I have fixed for you this time. Please use code tags on future posts. Thank You.
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]

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to replace matching rows SriMekala 2 74 Jul-13-2019, 09:43 AM
Last Post: SriMekala
  my openpyxl use is too slow, am I reading rows incorrectly? Clunk_Head 1 148 Jun-12-2019, 11:48 PM
Last Post: Clunk_Head
  Mysql returning number of rows not data AkaAndrew123 4 357 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123
  Pandas find the most often rows dervast 1 98 Jun-07-2019, 01:55 PM
Last Post: ichabod801
  maintaining list order 3Pinter 13 355 Jun-05-2019, 07:49 AM
Last Post: 3Pinter
  How to iterate over the pandas rows SriMekala 3 212 Jun-04-2019, 12:36 PM
Last Post: SriMekala
  Sqlite3 help with descending order I_Am_Groot 4 142 May-18-2019, 12:45 AM
Last Post: I_Am_Groot
  How to convert rows to columns. jarrod0987 1 147 May-15-2019, 02:58 AM
Last Post: scidam
  How get row index of matching row in column 0 Sri 7 316 Apr-10-2019, 08:26 PM
Last Post: perfringo
  How to add new rows to existing csv file Sri 1 177 Apr-08-2019, 05:49 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)