0

I am trying to append the prediction column from df_B to df_A.

df_A['prediction'] = df_B['prediction']

But the column wasn't properly copied and I got the following error:

/Users/edamame/workspace/git/tensorplay/venv/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """Entry point for launching an IPython kernel.

Is there a way to properly perform the column copy without looping over the data frame?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Edamame
  • 23,718
  • 73
  • 186
  • 320
  • 2
    You're doing the proper way. But `df_A` came from another dataframe, i.e. you likely at some point did `df_A = some_other_df[some_conditions]`. Now, whenever you change `df_A`, pandas will warn you saying you're changing a _copy_ of _some_other_df_, and not _`some_other_df`_ itself. To solve this, you might do `df_A = some_other_df[some_conditions].copy()` to make it independent from the previous df – rafaelc Aug 15 '19 at 18:43
  • I answered this here: https://stackoverflow.com/questions/57494760/create-duplicate-column-in-pandas-dataframe/57495064#57495064 – Ankur Sinha Aug 15 '19 at 18:44
  • 1
    another fix I like is to do `df_A = some_other_df[condition][:]` although it's not very robust – Yuca Aug 15 '19 at 18:45

1 Answers1

0

If the shape of df_A and df_B is the same, you can use pd.concat() functionality from Pandas.

import pandas as pd 

data_A = [['tom', 10], ['nick', 15], ['juli', 14]] 
df_A = pd.DataFrame(data_A, columns = ['Name', 'Age']) 

data_B = [['LA', "M"], ['NY', 'M'], ['SF', 'F']] 
df_B = pd.DataFrame(data_B, columns = ['City', 'Gender']) 

df_B = pd.concat([df_B, df_A[['Name']]], axis = 1)
Karan Arya
  • 104
  • 8