当一个列是整数/布尔值,Python3时,Pandas显示了SettingWithCopyWarning

时间:2017-08-24 06:43:44

标签: python-3.x pandas boolean

我在Python 3上使用Pandas包有以下代码。

import pandas as pd

def run(df_raw):
    df = df_raw
    mask = df['C'].idxmax()
    df_max = df.loc[mask]
    df_max['mask by maximum C'] = mask
    return df_max

def main():
    data = {
            'A': [1, 2],  # 'A' contains integers
            'B': [1, 0],  # 'B' contains boolean
            # 'B': [1., 0],  # 'B' contains floats
            'C': [0.520073, 0.544790]  # 'C' contains floats
            }

    df_raw = pd.DataFrame(data)
    df_raw = df_raw.set_index(['A'])

    df_max = run(df_raw)

    print(df_max)

if __name__ == '__main__':
    main()

对于列' B',如果它是布尔值,则SettingWithCopyWarning将显示,关于代码

df_max['mask by maximum C'] = mask

警告的完整信息是:

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
  df_max['mask by maximum C'] = mask
C:\Users\lei.wu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py:747: 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
  self.loc[key] = value

但是当我改变专栏“B'浮动,没有显示警告。

你知道原因是什么吗?感谢

0 个答案:

没有答案