Python Forum
Copy a column from one dataframe to another dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy a column from one dataframe to another dataframe
#11
df1=df does not copy df into a new dataframe df1, despite that being what it looks like. Instead, you simply have 2 variables pointing to the same dataframe. You need to use the copy() command

See Documentation
Reply
#12
df1 = df does not make a new dataframe. df1 and df are two variables that reference the same dataframe object. I will not go as far as saying assignment never makes a copy, but I will say that I have never seen it.
Reply
#13
You are correct and I am wrong. I earned the way t copy a dataframe (deep copy actually) and have it stand alone and not be changed, by any
changes to the original template.

It works perfectly.


However, the final command which was given to me in this series of posts it still throwing errors. The error is:

Error:
ndexError Traceback (most recent call last) Input In [14], in <cell line: 1>() ----> 1 df["machine_status"] = df1["machine_status"] IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I am still not getting the line to work. What is the cause of this error?

Thanks in advance.

Respectfully,

LZ
Reply
#14
Either df or df1 (or both) is not a DataFrame. What are their types? Looks like one (at least, but guessing df) is a numpy array.
Reply
#15
Yes, but how to determine if df or df1 is a dataset?

I am assuming dtype() function will do it. But I do not know the syntax.

Respectfully,

LZ
Reply
#16
Use the built-in the type() function. print(type(df), type(df1))
Reply
#17
You are correct. df is a numpy array. How did you know?
Reply
#18
It was in the error message:
Error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Lots of things can have [integer] or slices, but not many things that aren't numpy will accept a numpy.newaxis as an argument. Even without that I don't know many things that accept "[...]".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 578 Apr-24-2025, 01:05 PM
Last Post: snippsat
  renaming a column without a name in a dataframe Carbonpony 2 1,070 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  dataframe merge gunther 2 637 Jan-22-2025, 05:23 PM
Last Post: Pedroski55
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,403 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  Most efficient way to roll through a pandas dataframe? sawtooth500 2 1,179 Aug-28-2024, 10:08 AM
Last Post: Alice12
  Confused by the different ways of extracting data in DataFrame leea2024 1 713 Aug-17-2024, 01:34 PM
Last Post: deanhystad
  docx file to pandas dataframe/excel iitip92 1 2,770 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries sawtooth500 14 8,376 Apr-24-2024, 01:42 AM
Last Post: sawtooth500
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,374 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,808 Mar-29-2024, 05:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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