"尝试在DataFrame"的切片副本上设置值。即使使用.loc

时间:2016-06-30 20:47:49

标签: python python-2.7 pandas indexing dataframe

如果我使用.loc创建another_df数据框

,我不明白为什么会发生这种情况
>>> df = DataFrame({'a':range(0,10), 'b':range(10,20), 'c':range(20,30)}, index = range(0,10), columns=['a', 'b', 'c'])
>>> df
   a   b   c
0  0  10  20
1  1  11  21
2  2  12  22
3  3  13  23
4  4  14  24
5  5  15  25
6  6  16  26
7  7  17  27
8  8  18  28
9  9  19  29
>>> another_df =  df.loc[(df.a>4)&(df.b>14)&(df.c>24),:]
>>> another_df
   a   b   c
5  5  15  25
6  6  16  26
7  7  17  27
8  8  18  28
9  9  19  29
>>> another_df['d'] = 'a random string'
<string>: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

2 个答案:

答案 0 :(得分:1)

public string PhoneNumber
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            Regex regexObj = new Regex(@"[^\d]");                
            _phoneNumber = regexObj.Replace(value, "");               
            var match = Regex.Match(_phoneNumber,  @"(\d{3})(\d{3})(\d{4})");
            if(match.Success)
            {
                _phoneNumber = string.Format("({0}) {1}-{2}", match.Groups[1], match.Groups[2], match.Groups[3]);
                this.Areacode = match.Groups[1].ToString();
            }
        }
    }
    private string _phoneNumber;

    public string Areacode { get; private set; }

返回df.loc[(df.a>4)&(df.b>14)&(df.c>24),:]

的视图或副本

使用df

copy

答案 1 :(得分:-1)

您需要指定要为新列'a random string'获取值'd'的行。或者,您可以为列提供长度为len(another_df)的元素列表:

another_df['d']=['hello', 'world', 'hello', 'world', 'hello']