<p>我想写一个函数,它接收Panda数据帧和一个给定序列的名称,并检索这个序列的唯一值及其在数据集中的频率。</p>
<pre><code>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
</code></pre>
<p>然后,我可以按如下方式调用此函数:</p>
^{pr2}$
<p>有可能吗?
我试图连接字符串并使用eval()函数,但我不确定这是正确的方法。</p>