0

Considering I have the following dataframe

import pandas as pd
purchase_1 = pd.Series({'Name': 'Chris',
                        'Item Purchased': 'Dog Food',
                        'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
                        'Item Purchased': 'Kitty Litter',
                        'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
                        'Item Purchased': 'Bird Seed',
                        'Cost': 5.00})
df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])

what is the difference between df_slice = df.loc['Store 1', 'Cost'] and df_slice = df['Cost']['Store 1']? When I change the value of these slices, the original dataframe is modified in both cases. Although when I try to change the value of the slice from the first option, it raises an error when ran in Jupyter Notebook: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

  • this is discussed [here](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – anky Jun 26 '20 at 17:28

1 Answers1

0

The second method is an example of chained indexing, which can result in copy warnings / failures in some cases. In this page, the problem explained clearly.

Alihan ÖZ
  • 173
  • 1
  • 7