简体   繁体   中英

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)()

How does one apply methods such as describe and count that work on a series instead of each element of the series?

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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