计算数据框中所有分类变量的频率和频率百分比 Python

Calculating Frequency and % of frequency for all categorical variables in a data frame Python

我是 python 的新手,我正在处理一项要求,列出分类列中的所有唯一值以及每个值的频率和列中每个值的频率百分比,并使用 for循环以在完整数据集上执行它。此外,我不确定是否必须根据所附的屏幕截图使用 pd.Series 将数据附加到数据框中,因为列的长度因列中的唯一值而异。

感谢您的帮助。

下面是我尝试计算的代码,但我无法在其他列上计算唯一值和频率百分比并将其创建为数据框,以便我可以将其导出到 CSV

Count_df = []
for item in df.columns:
    Count_df_ = pd.DataFrame(df1[item].value_counts())
    Count_df.append(Count_df_)
Count_dfdf = pd.DataFrame(Count_df)
Count_dfdf
Count_dfdf.to_csv(path_or_buf = Output + '_' + 'Count_.csv')

预期的输入和输出如下,并附上:

[输入数据和预期输出][1]

提前致谢

没有魔法。只需耐心地附加输出 DataFrame column-by-column。

这里我假设在单个 .csv 文件中有 4 列输出。根据个人工作经验,这种格式比电子表格软件的单独文件更方便。但是,在循环中也可以分离输出。

代码:

import pandas as pd

# please provide copy-able sample data next time
df = pd.DataFrame(
    data={
        "Name": ["A", "B", "C", "C", "A", "F"],
        "col2": [True, False, False, False, False, True],
        "col3": [1, 2, 3, 1, 1, 3],
    }
)

# Construct an empty dataframe with convenient column order.
# The ordering can be adjusted later on.
df_ans = pd.DataFrame(
    data={
        "var_name": [],
        "var_count": [],
        "var_freq": [],
        "col_name": [],
    }
)

# process each column
for col in df.columns:

    # get variable name and count
    df_col_count = df[col].value_counts().to_frame().reset_index()
    # rename columns
    df_col_count.columns = ["var_name", "var_count"]

    # compute frequency
    df_col_count["var_freq"] = df_col_count["var_count"] / df_col_count["var_count"].sum()

    # append column name
    df_col_count["col_name"] = col

    # sort (optional)
    # (1) by name
    df_col_count.sort_values(by="var_name", inplace=True)
    # (2) by descending frequency
    # df_col_count.sort_values(by="var_freq", ascending=False, inplace=True)

    # append
    df_ans = df_ans.append(df_col_count)

    # For separated CSV output, output here (and "col_name" can be removed)
    #df_col_count.to_csv(f"/path/to/{col}_freq.csv")

# reorder columns
df_ans = df_ans[["col_name", "var_name", "var_count", "var_freq"]]
# reindex
df_ans.reset_index(drop=True, inplace=True)

# write csv
# df_ans.to_csv(f"/path/to/all_freq.csv")

输出

# Each column (variable) is sorted by name.
df_ans   

Out[12]: 
  col_name var_name  var_count  var_freq
0     Name        A        2.0  0.333333
1     Name        B        1.0  0.166667
2     Name        C        2.0  0.333333
3     Name        F        1.0  0.166667
4     col2    False        4.0  0.666667
5     col2     True        2.0  0.333333
6     col3        1        3.0  0.500000
7     col3        2        1.0  0.166667
8     col3        3        2.0  0.333333