python - 随机分配 pandas DataFrame 功能

标签 python python-2.7 matplotlib pandas

我正在使用 pandas 读取一组数据并使用 matplotlib 绘制它。一列是一个“类别”,例如“体育”、“娱乐”,但对于某些行,它被标记为“随机”,这意味着我需要分配该值并将其随机添加到一列。理想情况下,我想在数据框中执行此操作,以便分发所有值。

我的基本图形代码如下:

df.category.value_counts().plot(kind="barh", alpha=a_bar)
title("Category Distribution")

我想要的行为是

If category == "Random"{
   Assign this value to another column at random.
} 

我怎样才能做到这一点?

最佳答案

可能:

# take the original value_counts, drop 'Random'
ts1 = df.category.value_counts()
rand_cnt = ts1.random
ts1.drop('Random', inplace=True)

# randomly choose from the other categories
ts2 = pd.Series(np.random.choice(ts1.index, rand_cnt)).value_counts()

# align the two series, and add them up
ts2 = ts2.reindex_like(ts1).fillna(0)
(ts1 + ts2).plot(kind='barh')

如果你想修改原始数据框,那么

idx = df.category == 'Random'
xs = df.category[~idx].unique()  # all other categories

# randomly assign to categories which are 'Random'
df.category[idx] = np.random.choice(xs, idx.sum())

关于python - 随机分配 pandas DataFrame 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286310/

相关文章:

python - 如何将pandas时间序列图中的xticks更改为每年间隔

python - 绘图遮盖了 matplotlib 中的其他绘图

python - 与列表相比,从数据框中提取字符串

python - 来自while循环的语法错误

python - 如何在 python 中编写没有任何定界符的文本文件?

python - 如何在 python 中从三个列表创建热图?

python - 如何绘制 scikit learn 分类报告?

python - 连接层的 ValueError(Keras 函数式 API)

python - Ubuntu pyautogui .screenshot() 返回黑屏图像

python - 为什么 "counting sort"不是更广泛使用的算法?