ptp()的使用
https://vimsky.com/examples/usage/python-pandas-series-ptp.html
按照这个网址操作的 想问问为什么?
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
没问题
# return the difference between the
# maximum and the minimum value
result = sr.ptp()
# Print the result
print(result)
报错
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[2], line 3
1 # return the difference between the
2 # maximum and the minimum value
----> 3 result = sr.ptp()
5 # Print the result
6 print(result)
File E:\anaconda3\lib\site-packages\pandas\core\generic.py:5902, in NDFrame.__getattr__(self, name)
5895 if (
5896 name not in self._internal_names_set
5897 and name not in self._metadata
5898 and name not in self._accessors
5899 and self._info_axis._can_hold_identifiers_and_holds_name(name)
5900 ):
5901 return self[name]
-> 5902 return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'ptp'
【以下回答由 GPT 生成】
报错原因是AttributeError: 'Series' object has no attribute 'ptp'
。这是因为Pandas的Series对象没有ptp方法。
可以通过使用numpy库的ptp函数来计算Series的最大值和最小值之间的差值。下面是修改后的代码示例:
# importing pandas and numpy
import pandas as pd
import numpy as np
# Creating the Series
sr = pd.Series([10, 25, 3, 11, 24, 6])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the index
sr.index = index_
# Print the series
print(sr)
# return the difference between the maximum and the minimum value
result = np.ptp(sr)
# Print the result
print(result)
这样就可以成功计算Series的最大值和最小值之间的差值了。
属性错误:Series没有ptp()方法!
您的代码,如果是在同一个文件中,应该没有问题才对啊!但为啥“出事儿”了哩?😂
您列出的两段代码真在“一块儿”么?