0

I want to assign the group name of a groupby result back to each entry in a dataframe.

# filter the dataframe
df_car = df[(df["Vehicle"] == "3960") & (df["Measurement Type"] == "FE") & (df["Measurement Location"].isin(list('ABC')))]
# group it
df_car_grouped = df_car.groupby(["Seat row", "Measurement Location"])
# assign the group for each entry
df_car['Group'] = None
for label, group in df_car_grouped:
    print(label, group.index)
    df_car.loc[group.index, 'Group'] = "{} {}".format(*label)

But i am getting a SettingWithCopyWarning although i am using .loc here. But i don't understand why.

This is the output in my jupyter notebook:

...\lib\site-packages\ipykernel\__main__.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/indexing.html#indexing-view-versus-copy
  if __name__ == '__main__':

(('Front', u'A'), Int64Index([ 88,  89,  90,  91,  92,  93, 126, 127, 128, 129, 130, 131, 132,
            133, 134, 150, 151, 152, 153, 154, 155, 156, 157, 192, 193, 194,
            195, 196, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 248,
            249, 250, 251, 252, 253, 254, 255],
           dtype='int64'))
(('Front', u'B'), Int64Index([ 94,  95,  96,  97,  98, 135, 136, 137, 138, 139, 140, 141, 142,
            143, 144, 145, 146, 147, 148, 149, 164, 165, 166, 167, 168, 197,
            198, 199, 200, 201, 239, 240, 241, 242, 243, 244, 245, 246, 247,
            256, 257, 258, 259, 260, 261, 262],
           dtype='int64'))
...

Where do i get a copy in this code?

If i check the df_car dataset the 'Group' column is filled correctly.

SOLUTION: Put a .copy() at the end of the df_car =... line as this is the intended behaviour.

maggie
  • 3,935
  • 3
  • 27
  • 31
  • Check [this question](https://stackoverflow.com/questions/45170312/pandas-settingwithcopywarning?rq=1); this might help. – Cleb Nov 28 '17 at 11:12
  • According to this question the problem is `df_car = df[(df["Vehicle"]...`. But my intention is to create a filtered dataset `df_car` which shall not be a view on df but a copy. So actually i just can ignore this warning as it is intended or use copy() to silence it. – maggie Nov 28 '17 at 12:08
  • As i thought the problem is in the .loc line, the upper assignment was not in my focus. The pandas documentation especially states that in http://pandas.pydata.org/pandas-docs/stable/indexing.html#why-does-assignment-fail-when-using-chained-indexing at the end of the paragraph. – maggie Nov 28 '17 at 12:12
  • Yes, if you add a `.copy` there it shall all be fine. :) – Cleb Nov 28 '17 at 12:26

0 Answers0