我想将groupby结果的组名称分配回数据框中的每个条目。
# 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)
但是我得到了一个SettingWithCopyWarning,虽然我在这里使用.loc
。但我不明白为什么。
这是我的jupyter笔记本中的输出:
...\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'))
...
我在哪里可以获得此代码的副本?
如果我检查df_car
数据集,'Group'
列已正确填充。
解:
将.copy()
放在df_car =...
行的末尾,因为这是预期的行为。