![]() |
Copy a column from one dataframe to another dataframe - 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: Copy a column from one dataframe to another dataframe (/thread-37665.html) Pages:
1
2
|
Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 I would like to copy a column from one dataframe and add it to a second dataframe as the last column. I know it can be done, but I am looking for a simple way to do it. Is there a one, two or three code line way to do it? This is all using Panda and Python, of course. Any help appreciated. Thanks in advance. Respectfully, LZ RE: Copy a column from one dataframe to another dataframe - deanhystad - Jul-07-2022 dataframe_a['New Column'] = dataframe_b['Existing Column'] RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 Thanks, I will give it a try. Respectfully, LZ RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 I tried the simple command that you gave me: df['machine_status'] = df1['machine_status']Now df is the new dataframe and df1 is the old copied dataframe. I am trying to move the column named 'machine_status', from df1 to df and put it in the last column. The column is named 'machine_status' in both cases. I got the following error: Where is the error? I think that I entered the python code correctly. The python interpreter says that I did not.What is a key error? Thanks in advance. Respectfully, LZ . RE: Copy a column from one dataframe to another dataframe - deanhystad - Jul-07-2022 Is "machine_status" a key (column header) in df1? This is how things work in theory import pandas as pd df1 = pd.DataFrame({"Letters": ["A", "B", "C", "D"]}) df2 = pd.DataFrame({"Numbers": [1, 2, 3, 4]}) df1["Integers"] = df2["Numbers"] # df1 can be any name. df2 has to be existing column print(df1)
RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 Yes, it is. I took three columns off df dataframe, but before I did that, I made a copy of df dataframe and called it df1. I am trying to put machine status back on to the slimmed down dataframe, df. I have done all of my scaling and normalizing on df, so now I am trying to put it back together. I want to reverse the dropping of three columns, but only for machine_statis column now. That is where it fails. df is only a slimmed down column of its former self. But "machine_statis" is the name of the column I have been discussing. I hope this is informative. Respectfully, LZ RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 I like your example. But please understand, doing it that way would be very hard. My machine_status column has over 220,000 values. It is not practical. I really want to use the short cut method, but I keep getting this "key_error". That is why I chose the shorthand way. I will keep trying. R, LZ RE: Copy a column from one dataframe to another dataframe - deanhystad - Jul-07-2022 It doesn't make any difference how many "values" there are. The dataframe operations shown are working with series (columns). A column containint 20,000 values works exactly as one containing 4. This part is just so I have two dataframes to work with. df1 = pd.DataFrame({"Letters": ["A", "B", "C", "D"]}) df2 = pd.DataFrame({"Numbers": [1, 2, 3, 4]})If you prefer they could each have 1,000,000 values. I get an error when I do this: import pandas as pd df1 = pd.DataFrame({"Letters": ["A", "B", "C", "D"]}) df2 = pd.DataFrame({"Numbers": [1, 2, 3, 4, 5]}) df1["Integers"] = df2["Number"] print(df1) That looks a lot like your error. I think (am quite sure) there is no "machine_status" column in df1.
RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 You know that is what I thought also. I will check it and see. Thanks for your input. Respectfully, LZ RE: Copy a column from one dataframe to another dataframe - Led_Zeppelin - Jul-07-2022 You are correct. But it should be there. In an earlier line in the program, I used the command. df1=df Now I did this before I dropped the three columns from df. It ("machine_status") should be there, but it is not. How can I work a round this. I wanted to keep a dataframe from my initial uploading. The I can work on the initial dataframe to scale and normalize so I can then "attach" the dropped three columns at a later time. However, this did not work out and the error is in ddf1 = df Any help appreciated. Respectfully, LZ |