簡體   English   中英

dataframe 列的單獨值計數

[英]Separate count of values for dataframe column

我最近查看了使用 pandas GroupBy 獲取每個組的統計信息(例如計數、平均值等)? . 它沒有解決我的問題。

有沒有辦法為多個 df 列單獨計算值?

查看我的代碼:

Import pandas as pd
Import numpy as np
fle1 = r’k:\file1.xlsx’

df1 = pd.read_excel(fle1, sheet_name=”Sheet1”)
df2 = df1.select_dtypes(exclude=np.number)
col_names = df2.columns.values.tolist()

col_names包含['Column1', 'Column2', 'Column3']

我的 dataframe 看起來像。

Column1     Column2          Column3  
Checking    Car              House  
Checking    Car              House  
Checking    Car              House  
House       Checking         Car 
House       Checking         Car 

我正在尋找 output ,例如:

Column1   Count  Column2    Count   Column3 Count
Checking    3    Car        3       House    3
House       2    Checking   2       Car      2

不知道下一步是什么,任何幫助將不勝感激。

您可以使用pd.concatgroupby

(
    pd.concat([df[x].groupby(df[x]).size().to_frame('Count').reset_index() 
               for x in df.columns],1)
)

    Column1     Count   Column2     Count   Column3 Count
0   Checking    3       Car         3       Car     2
1   House       2       Checking    2       House   3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM