使用.mean(),.median(),.mode()在python上的python in pandas dataframe上的删除其他索引

发布于 2025-01-23 03:41:57 字数 976 浏览 0 评论 0原文

),.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

征棹 2025-01-30 03:41:57

这将修复它,

mode = dataframe['frequency_mhz'].mode().values[0]

mode()函数还给Pandas系列还原。因此,这将使您可以访问该系列中的项目。

This would fix it,

mode = dataframe['frequency_mhz'].mode().values[0]

The mode() function gives back a pandas series. So this would allow you to access the item in that series.

何必那么矫情 2025-01-30 03:41:57

将PANDAS变成一个Numpy数组。

mode = dataframe['frequency_mhz'].mode().values

您可以使用.values属性:应该为您提供所需的东西,

You can turn a pandas into a numpy array using the .values property:

mode = dataframe['frequency_mhz'].mode().values

should give you what you want.

脱离于你 2025-01-30 03:41:57

因为 >可以返回一个或多个值,需要标量的过滤器值:

该模式是最经常出现的值。可以有多种模式。

print("The most common freq of large airports is", mode.iat[0])

Because Series.mode can return one or more values, need filter first value for scalar:

The mode is the value that appears most often. There can be multiple modes.

print("The most common freq of large airports is", mode.iat[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文