pandas 1.1.0 警告如下:
D:\Users\*****\AppData\Roaming\Python\Python37\site-packages\pandas\core\frame.py:4317: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
downcast=downcast,
Troubleshooting
已经给出提醒了,参考 官方文档
In [338]: dfmi = pd.DataFrame([list('abcd'),
.....: list('efgh'),
.....: list('ijkl'),
.....: list('mnop')],
.....: columns=pd.MultiIndex.from_product([['one', 'two'],
.....: ['first', 'second']]))
.....:
In [339]: dfmi
Out[339]:
one two
first second first second
0 a b c d
1 e f g h
2 i j k l
3 m n o p
In [340]: dfmi['one']['second']
Out[340]:
0 b
1 f
2 j
3 n
Name: second, dtype: object
上面代码就是我用的访问dataframe方式,这是链式chained,另一种是用loc方法。
In [341]: dfmi.loc[:, ('one', 'second')]
Out[341]:
0 b
1 f
2 j
3 n
Name: (one, second), dtype: object
官方评语如下:
These both yield the same results, so which should you use? It is instructive to understand the order of operations on these and why method 2 (.loc) is much preferred over method 1 (chained []).
意思就是用loc函数比链式好。