1
我有一个我希望根据频率进行分类的城市名称列表。我首先想要使用binning,但是因为这需要单调的间距,所以我放弃了这一点。接下来,甚至更好的方法是使用pandas.qcut根据频率创建基于分位数的类别。但拥有分位数,我不知道如何根据分位数创建一个额外的列。例如:如何根据频率对文本列进行分类
import numpy as np
import pandas as pd
np.random.seed(0)
cities = np.random.choice(['Ontario', 'Ottawa', 'Vancouver','Edmonton',
'Winnipeg', 'Churchill'], 500)
# Create fake data and their frequencies
df = pd.DataFrame (cities, columns=['City'])
freq = df['City'].value_counts()
print (freq)
# Create quantiles
qc = pd.qcut (freq, 3)
print (qc)
# And now? I have the quantiles but how to assign a categorie to each City?
category_for_each_city = df['City'] in qC# does not work, but many other things neither
我尝试了很多事情,但都没有成功。我应该能够为此编写一个循环,但我无法想象这是Python的方式。我试图寻找一些sklearn变形金刚,但无法找到任何与此特定的解决方案。任何帮助将不胜感激。
此外,我有很多倾斜的分布,可以扩展到例如日志转换的解决方案将有很大的帮助。
这很容易的确。我正在尝试类似'qc.category_for_each_city.codes [df ['City']]',但您的解决方案要简单得多。非常感谢你的帮助! – Arnold