简体   繁体   English

如何将以系列为参数的函数应用于pandas列

[英]How to apply a function with series as argument to pandas column

I want to create a function that essentially does the following: 我想创建一个基本上执行以下操作的函数:

def displayDetails(dataframe,column):
    dataframe.column.describe()
    dataframe.column.value_counts.display(kind='bar')

so that I can do displayDetails(someDataFrame,someColumn)() 这样我就可以做displayDetails(someDataFrame,someColumn)()

How does one apply methods such as describe and count that work on a series instead of each element of the series? 如何应用诸如describecount方法对系列而不是系列的每个元素起作用?

I tried this: 我尝试了这个:

def displayDetailsDummy(para):
    para.upper()
df4['Clinic_ID'].map(displayDetailsDummy)

which generated the output: 生成了输出:

000103f8-7f48-4afd-b532-8e6c1028d965    None
00021ec5-9945-47f7-bfda-59cf8918f10b    None
0002510f-fb89-11e3-a6eb-742f68319ca7    None
00025550-9a97-44a4-84d9-1f6f7741f973    None

This output is incorrect. 此输出不正确。

Another approach is: 另一种方法是:

df4['Clinic_ID'].map(displayDetailsDummy)
test = map(vis, df4['Clinic_ID'])
print test

This one works: 这个作品:

[u'43E75091BE4CA5FF8182A9D45FD654E0', u'E2A62C2E0EB48FA046B6407DBE633856', u'4001', u'43E75091BE4CA5FF8182A9D45FD63870', u'05015141289349B8BC84533C7B489114']

However, using it for value_counts and describe throws up errors. 但是,将其用于value_countsdescribe会引发错误。

Solved it: 解决了:

def displayDetails(df_name,col_name):
    print df_name[col_name].describe()
    df_name[col_name].value_counts().plot(kind='bar')

displayDetails(dataFrameName,'Column_Name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM