1

First time posting here - have decided to try and learn how to use python whilst on Covid-19 forced holidays.

I'm trying to summarise some data from a pretty simple database and have been using the value_counts function.

Rather than running it on every column individually, I'd like to loop it over each one and return a summary table. I can do this using df.apply(pd.value_counts) but can't work out how to enter parameters into the the value counts as I want to have dropna = False.

Basic example of data I have:

# Import libraries 
import pandas as pd 
import numpy as np

# create list of winners and runnerup
data = [['john', 'barry'], ['john','barry'], [np.nan,'barry'], ['barry','john'],['john',np.nan],['linda','frank']] 

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['winner', 'runnerup']) 

# print dataframe. 
df

How I was doing the value counts for each column:

#Who won the most?
df['winner'].value_counts(dropna=False)

Output:
john     3
linda    1
barry    1
NaN      1
Name: winner, dtype: int64

How can I enter the dropna=False when using apply function? I like the table it outputs below but want the NaN to appear in the list.

#value counts table
df.apply(pd.value_counts)
      winner    runnerup
barry   1.0       3.0
frank   NaN       1.0
john    3.0       1.0
linda   1.0       NaN

#value that is missing from list
#NaN    1.0       1.0

Any help would be appreciated!!

4

2 回答 2

0

您可以使用df.apply,如下所示:

df.apply(pd.value_counts, dropna=False)
于 2020-04-03T23:06:36.533 回答
0

在 pandas apply 函数中,如果只有一个参数,您只需执行以下操作:

.apply(func_name)

参数是单元格的值。这对于 pandas 内置函数和用户定义函数 (UDF) 的工作方式完全相同。

对于UDF,当有多个参数时:

.apply(func_name, args=(arg1, arg2, arg3, ...))

见:这个链接

于 2020-04-03T23:35:36.330 回答