设置行中选定行的值

时间:2019-06-19 14:15:24

标签: pandas

尝试设置使用.isin选择的行的值时,出现“试图从DataFrame的切片副本上设置值”的错误。

import pandas as pd
import numpy as np

df = pd.DataFrame({'Category': ['Men Open', 'Men 60 years +', 'Women Open', 'Men 50-59 years',
                                'Women 40-49 years', 'Men 40-49 years', 'Women 50-59 years', 'Women 60 years +'], 
                   'Athlete': [1, 2, 3, 4, 5, 6, 7, 8]
                  })
male_values = ['Men Open', 'Men 60 years +', 'Men 50-59 years', 'Men 40-49 years']
df.head(8)

# Create new column and set all values to female
df['Gender'] = 'Female'

# Select the rows I want to change
df.loc[df['Category'].isin(male_values)]

# Try to set the Gender column to 'Male' for the selection of rows
df.loc[df['Category'].isin(male_values)].loc['Gender'] = 'Male'

然后我得到这个错误:

C:\Users\laure\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

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

我假设我应该添加另一个.loc,但无法弄清楚

0 个答案:

没有答案