我对Python环境非常陌生。我创建了一个包含列名,数据值及其频率计数(最高计数)的列表。但是,我只想在输出中保留列名,日期值和频率。此输出可能是数据框,列表或字典
任何帮助将不胜感激。预先感谢
我尝试使用reset_index()或to_string(header = None),但它删除了所有内容
df_vec = [(df.loc[:, x].value_counts().head(1)) for x in df.columns]
[160.0 1
Name: Unnamed: 0, dtype: int64, 160.0 1
Name: Site.No., dtype: int64, Wakuia Wai 2
Name: Site.Name, dtype: int64, * 48
Name: Site.Code, dtype: int64, New Ireland 24
Name: District, dtype: int64, PNG 31
Name: Island, dtype: int64, PNG 74
Name: Country, dtype: int64, E 67
Name: Technique, dtype: int64, igneous 48
Name: Geology, dtype: int64, boulder 46
Name: Topography, dtype: int64, cliffed 24
Name: Location, dtype: int64, * 76
Name: Proxhab.km., dtype: int64, AN 54
Name: Language, dtype: int64, * 98
Name: No.motif, dtype: int64, 0 98
Name: Ca1, dtype: int64, 0 101
我需要类似的东西(最好在数据框中显示此结果)-
Name: Unnamed: 0 , 160.0 , 1
Site.No. , Wakuia Wai, 2
Site.Name , * , 48
答案 0 :(得分:0)
我认为您可以使用stack
而不是原始循环
df.stack().groupby(level=1).value_counts().groupby(level=0).head(1)
Out[205]:
Keys a 3
Reason x 3
dtype: int64
样本数据
Reason Keys
0 x a
1 y a
2 z a
3 y b
4 z b
5 x c
6 w d
7 x d
8 w d