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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help copying a column from a csv to another file with some extras g0nz0uk 3 456 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Copying the order of another list with identical values gohanhango 7 1,135 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 828 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,630 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,111 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,104 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to filter out Column data From Multiple rows data? firaki12345 10 5,093 Feb-06-2021, 04:54 AM
Last Post: buran
  Copying column values up based on other column values codelines 1 2,057 Jan-03-2021, 05:55 PM
Last Post: codelines
  how to combine rows to a column base on ids zhujp98 0 1,495 Nov-03-2020, 04:10 PM
Last Post: zhujp98
  How to generate rows based on values in a column to fill missing values codesmatter 1 2,123 Oct-31-2020, 12:05 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020