简体   繁体   English

计算Pandas系列浮标中的出现次数

[英]Count occurrences in a Pandas series of floats

I have a DataFrame: 我有一个DataFrame:

df.head()

Index                          Value
0                    1.0,1.0,1.0,1.0
1                            1.0,1.0
2                            1.0,1.0
3    3.0,3.0,3.0,3.0,3.0,3.0,4.0,4.0
4                                  4

I'd like to count the occurrences of values in the Value column: 我想计算“ Value列中值的出现次数:

Index                          Value   1    2    3    4
0                    1.0,1.0,1.0,1.0   4    0    0    0
1                            1.0,1.0   2    0    0    0
2                            1.0,1.0   2    0    0    0
3    3.0,3.0,3.0,3.0,3.0,3.0,4.0,4.0   0    0    6    2
4                                  4   0    0    0    1

I've done this before with string values but I used Counter - which I found you can't use with floats? 我之前使用字符串值完成了此操作,但是我使用了Counter我发现您不能将其与float一起使用?

df_counts = df['Value'].apply(lambda x: pd.Series(Counter(x.split(','))), 1).fillna(0).astype(int)

Use map to floats and last columns to integers : 使用map浮点数,最后一列为integers

df_counts = (df['Value'].apply(lambda x: pd.Series(Counter(map(float, x.split(',')))), 1)
                        .fillna(0)
                        .astype(int)
                        .rename(columns=int))
print (df_counts)
   1  3  4
0  4  0  0
1  2  0  0
2  2  0  0
3  0  6  2
4  0  0  1

Last if necessary add all missing categories add reindex and join to original: 最后,如有必要,添加所有缺失的类别,添加reindexjoin原始目录:

cols = np.arange(df_counts.columns.min(), df_counts.columns.max() + 1)
df = df.join(df_counts.reindex(columns=cols, fill_value=0))
print (df)
                                 Value  1  2  3  4
Index                                             
0                      1.0,1.0,1.0,1.0  4  0  0  0
1                              1.0,1.0  2  0  0  0
2                              1.0,1.0  2  0  0  0
3      3.0,3.0,3.0,3.0,3.0,3.0,4.0,4.0  0  0  6  2
4                                    4  0  0  0  1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM