我想写一个函数,它接收Panda数据帧和一个给定序列的名称,并检索这个序列的唯一值及其在数据集中的频率。
def getUniqueValuesByField(dataframe, fieldname):
''' Retrive for non-numerical series the unique values and their frequencies '''
result = dataframe.fieldname.apply(lambda x: pd.Series(x)).unstack().value_counts(normalize=True, sort=True, ascending=False, bins=None, dropna=True)
#dataframe[fieldname].unique()
return result
然后,我可以按如下方式调用此函数:
^{pr2}$有可能吗? 我试图连接字符串并使用eval()函数,但我不确定这是正确的方法。
按如下方式使用
.value_counts()
:相关问题 更多 >
编程相关推荐