简体   繁体   English

如何编写自定义函数来显示数据框中每个变量的值计数以及级别?

[英]How do i write a custom function to show value counts of each variable in a dataframe along with levels?

I have written some of code for the below but unable to get write code to display value counts for each levels我已经为下面的代码编写了一些代码,但无法编写代码来显示每个级别的值计数

def info(data):
    return pd.DataFrame({"Data Type":data.dtypes,
                         "No of Levels":data.apply(lambda x: x.nunique(),axis=0)})

Output输出

   Data Type     No of Levels        Levels
A   int64                 3         [1 2 3]
B   int64                 8         [1 2 3 4 5 6 7 8]
C   int64                 6         [1 2 3 4 5 6]
D   object                4         ['Apple' 'Mango' 'Grapes' 'Orange']
E   object                3         ['Cauliflower' 'Potato' 'Tomato']
F   int64                 2         [0 1]
G   int64                 2         [1 0]
H   int64                 2         [1 0]

Desired Output期望输出

   Data Type     No of Levels        Value Counts                  Levels
A   int64                 3       1:100; 2:150; 3:200            [1 2 3]
B   int64                 8       0: 100; 1:200; 3:300...    [1 2 3 4 5 6 7 8]
C   int64                 6       0: 100; 1:200; 3:300...    [1 2 3 4 5 6]
D   object                4       Apple:5,Mango:150...       ['Apple' 'Mango' 'Grapes' 'Orange']
E   object                3       Cauliflower:5,Potato:150...['Cauliflower' 'Potato' 'Tomato']
F   int64                 2       0: 100; 1:200              [0 1]
G   int64                 2       0: 100; 1:200              [1 0]
H   int64                 2       0: 100; 1:200              [1 0]

You can try to create a python dict as follows您可以尝试创建一个python dict如下

dict((x,l.count(x)) for x in set(data))

在此处输入图片说明

暂无
暂无

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

相关问题 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python? 如何获得数据框指定列中每个项目的值计数并仍保持索引? - How would do I get the value counts of each item in specified columns of my dataframe and still maintain the index? 我如何 plot 多行使用 dataframe 上的值计数 - how do i plot multiple lines using value counts on a dataframe 如何根据值计数过滤 pandas DataFrame? - How do I filter a pandas DataFrame based on value counts? 我如何编写一个函数或循环,将每个唯一值分配到它自己的可访问数据框中? - How could I write a function or loop that assigns each unique value into its own accessible dataframe? 如何在 Pandas Dataframe 的每一行底部添加类似 value_counts() 的内容? - How can I add something like a value_counts() at the bottom of each row in a Pandas Dataframe? Python:如何编写 function 以确定 dataframe 中的哪个变量与指定列的绝对相关性最高? - Python: How do I write a function to determine which variable in a dataframe has the highest absolute correlation with a specified column? 如何计算pyspark数据帧中每个不同值的计数? - How to calculate the counts of each distinct value in a pyspark dataframe? 如何在每一行上执行value_counts并创建一些其值为每个值的计数的列 - How to do value_counts on each row and make some columns whose values are the counts of each value 如何编写一个看起来像每个function_name的函数名称和变量参数列表的函数 - How do I write a function that looks like it takes a list of function names and variable arguments for each function_name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM