python - 需要函数源码从dataframe中获取数据;找到平均中位数和众数

标签 python pandas

尝试从数据帧中导出平均值、中位数和众数。我需要知道如何在函数中编写源代码而不是“:”。

来源 = [df.'DMC]

import pandas as pd
import nltk

df.head(4)
# This is the print out of the dataframe 
# When I came up with this code, the source was
# source=[3,4,6,4,7,2,6,7,...]
# But now I need to get the data from a dataFrame. 
#   X   Y   month   day   DMC    RH
# 0 7   5   3       fri   26.2   94.3
# 1 7   4   10      tue   90.6   35.4
# 2 6   6   12      mon   56.8   99.2
# this is just a sample

#This is the code to find the mean median and mode

source = [df:'DMC']  #This is were I need your help.
def meanmedianmode (source):
    mmm = {'mean': Mean(source), 'median': Median(source), 'mode':
            Mode(source) }
def Mean (source):
    mean = reduce(lambda x,y: x+y, numbers)/len(source)
    return mean

def Median(source):
    median = numpy.median(source)
    return(median)

def Mode (source):
    mode = statistics.mode(source)
    return mode
    return mmm
print("mean median mode" + str(meanmedianmode(source)))

最佳答案

要回答您的具体问题,为了选择 pandas 数据帧的特定列,您可以使用语法

source = df.DMC 

source = df['DMC']

但是,您不必费力实现自己的函数来查找均值、中位数和众数。值得庆幸的是,pandas 已经包含了所有这三个函数的函数。检查computations/descriptive statspandas 文档下。 解决方案很简单

In [6]: df = pd.DataFrame({'X':[7,7,6], 'DMC':[26.2, 90.6, 56.8]})

In [7]: df
Out[7]:
    DMC  X
0  26.2  7
1  90.6  7
2  56.8  6

In [8]: df.DMC.mean()
Out[8]: 57.86666666666667

In [9]: df.DMC.median()
Out[9]: 56.8

In [10]: df.DMC.mode()
Out[10]:
0    26.2
1    56.8
2    90.6
dtype: float64

关于python - 需要函数源码从dataframe中获取数据;找到平均中位数和众数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56533432/

相关文章:

python - 计算使用 pandas 数据框列的公式字符串

python - 从数据框中删除多列

python - Dataframe.rolling().mean不计算移动平均值

python - 导入云存储,ImportError : No module named google. appengine.api

python - 在 Python 中迭代打印两种数据类型

python - 如何在特定级别重新排序多索引数据框列

python - Pytest 装置范围

Python 包设置 : setup. py,自定义处理包装的 Fortran

python - SQL "partition by"Python/R 中的类似功能

python - Pandas groupby 滚动分位数