Python Forum
In consistency in code execution
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In consistency in code execution
#1
The following piece of code does not produce the output that I am seeking.

df.head()

df1 = df

df.drop(["timestamp","time_period","machine_status"], axis = 1)

df.head()
In the code I am taking a dataframe and then creating a second datafame with that is exactly the same as the first dataframe. I want to remove three columns from the first dataframe, perform some scaling mathematics on the slimmed down dataframe, and then replace the removed columns by taking them from the second dataframe and attaching them to the first. That way they are never involved in the mathematics of the numeric columns.

The end results should be the dataframe with all numeric columns scaled and the other columns reattached in the correct order giving me my desired dataframe.

However. something is wrong. I remove the columns and it gives me the datatframe output with the 3 columns removed, but when I ask to see the head of the data frame as shown below, they are back in the dataframe and I had not done anything, the last line gives the following output.

0	0	2018-04-01 00:00:00	Night	2.465394	47.09201	53.2118	46.310760	634.3750	76.45975	13.41146	...	41.92708	39.641200	65.68287	50.92593	38.194440	157.9861	67.70834	243.0556	201.3889	NORMAL
1	1	2018-04-01 00:01:00	Night	2.465394	47.09201	53.2118	46.310760	634.3750	76.45975	13.41146	...	41.92708	39.641200	65.68287	50.92593	38.194440	157.9861	67.70834	243.0556	201.3889	NORMAL
2	2	2018-04-01 00:02:00	Night	2.444734	47.35243	53.2118	46.397570	638.8889	73.54598	13.32465	...	41.66666	39.351852	65.39352	51.21528	38.194443	155.9606	67.12963	241.3194	203.7037	NORMAL
3	3	2018-04-01 00:03:00	Night	2.460474	47.09201	53.1684	46.397568	628.1250	76.98898	13.31742	...	40.88541	39.062500	64.81481	51.21528	38.194440	155.9606	66.84028	240.4514	203.1250	NORMAL
4	4	2018-04-01 00:04:00	Night	2.445718	47.13541	53.2118	46.397568	636.4583	76.58897	13.35359	...	41.40625	38.773150	65.10416	51.79398	38.773150	158.2755	66.55093	242.1875	201.3889	NORMAL
Why are these three columns still there and why am I getting two no name columns in the 1 and 2 place of the dataframe?

Any help appreciated.

Thanks.

Respectfully,

LZ
Reply
#2
df.drop does not remove rows or columns from df unless you set "inplace=True". What it does is creates a new dataframe that does not contain the dropped rows or columns.

https://pandas.pydata.org/pandas-docs/st....drop.html

The return value from Dataframe.drop()
Quote:Returns
DataFrame or None
DataFrame without the removed index or column labels or None if inplace=True.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different code execution times Wirbelwind94 4 857 Oct-06-2023, 12:30 PM
Last Post: snippsat
  Minimizing the CMD window during code execution Shaswat 1 4,695 Oct-03-2019, 07:44 AM
Last Post: Shaswat
  code execution after event shift838 3 2,903 Nov-26-2018, 05:10 AM
Last Post: Larz60+
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,841 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  Another working code, help required for faster multithreading execution anna 0 2,305 Feb-09-2018, 03:26 AM
Last Post: anna
  Help required for faster execution of working code anna 2 3,223 Feb-09-2018, 03:00 AM
Last Post: anna
  code to understand the script execution Skaperen 2 3,409 Jan-06-2018, 05:25 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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