AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘

2021/4/24 10:25:21

本文主要是介绍AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

问题原因

pandas版本0.20.0及其以后版本中,ix已经不被推荐使用

问题解决

使用loc和iloc替换

loc

loc gets rows (or columns) with particular labels from the index. loc从索引中获取具有特定标签的行(或列)。这里的关键是:标签。标签的理解就是name名字。

>>> import pandas as pd
>>> df = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
>>> print(df.loc['e'])
a    1
b    2
c    3
Name: e, dtype: int64
>>>   
iloc

iloc gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置获取行(或列)(因此它只接受整数)。这里的关键是:位置。位置的理解就是排第几个。

>>> import pandas as pd
>>> df = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
>>> print(df.iloc[1])
a    4
b    5
c    6
Name: f, dtype: int64


这篇关于AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程