![]() |
In consistency in code execution - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: In consistency in code execution (/thread-37570.html) |
In consistency in code execution - Led_Zeppelin - Jun-26-2022 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 NORMALWhy 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 RE: In consistency in code execution - deanhystad - Jun-27-2022 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/stable/reference/api/pandas.DataFrame.drop.html The return value from Dataframe.drop() Quote:Returns |