使用.mean(),.median(),.mode()在python上的python in pandas dataframe上的删除其他索引
),.mode()计算熊猫df列的模式/中位数/平均值
def largeStats(dataframe):
dataframe.drop(dataframe.index[dataframe['large_airport'] != 'Y'], inplace=True)
mean = dataframe['frequency_mhz'].mean()
mode = dataframe['frequency_mhz'].mode()
median = dataframe['frequency_mhz'].median()
print("The mean freq of large airports is", mean)
print("The most common freq of large airports is", mode)
print("The middle freq of large airports is", median)
print(largeStats(df))
(
The mean freq of large airports is 120.00752293577986
The most common freq of large airports is 0 121.75
1 122.10
dtype: float64
The middle freq of large airports is 121.85
None
我正在使用.mean(),. median 每个号码:
大型机场的平均频率为120.00752293577986
最常见的大型机场的频率为121.75& 122.10
大型机场的中间频率为121.85
我知道由于2个模式值而到位,但是如何删除该索引?
I am calculating the mode/median/mean of pandas df columns using .mean(), .median(), .mode() but when doing so an index appears in some of the results:
def largeStats(dataframe):
dataframe.drop(dataframe.index[dataframe['large_airport'] != 'Y'], inplace=True)
mean = dataframe['frequency_mhz'].mean()
mode = dataframe['frequency_mhz'].mode()
median = dataframe['frequency_mhz'].median()
print("The mean freq of large airports is", mean)
print("The most common freq of large airports is", mode)
print("The middle freq of large airports is", median)
print(largeStats(df))
returns:
The mean freq of large airports is 120.00752293577986
The most common freq of large airports is 0 121.75
1 122.10
dtype: float64
The middle freq of large airports is 121.85
None
I want it to simply return the number for each:
The mean freq of large airports is 120.00752293577986
The most common freq of large airports is 121.75 & 122.10
The middle freq of large airports is 121.85
I know the indexing is in place due to 2 mode values but how would I remove that indexing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将修复它,
mode()
函数还给Pandas系列还原。因此,这将使您可以访问该系列中的项目。This would fix it,
The
mode()
function gives back a pandas series. So this would allow you to access the item in that series.将PANDAS变成一个Numpy数组。
您可以使用
.values
属性:应该为您提供所需的东西,You can turn a pandas into a numpy array using the
.values
property:should give you what you want.
因为 >可以返回一个或多个值,需要标量的过滤器值:
Because
Series.mode
can return one or more values, need filter first value for scalar: