python - 如何用随机字典值填充 pandas 数据框列

标签 python pandas dictionary dataframe random

我是 Pandas 新手,我想使用随机文本数据。我正在尝试向 DataFrame df 添加 2 个新列,每个列都由从字典中随机选择的键 (newcol1) + 值 (newcol2) 填充。

countries = {'Africa':'Ghana','Europe':'France','Europe':'Greece','Asia':'Vietnam','Europe':'Lithuania'}

我的 df 已经有 2 列,我想要这样的东西:

    Year Approved Continent    Country
0   2016      Yes    Africa      Ghana
1   2016      Yes    Europe  Lithuania
2   2017       No    Europe     Greece

我当然可以使用 for 或 while 循环来填充 df['Continent'] 和 df['Country'] 但我感觉 .apply() 和 np.random.choice 可能会为此提供一个更简单、更可扩展的解决方案。

最佳答案

是的,你说得对。您可以将 np.random.choicemap 一起使用:

df

    Year Approved
0   2016      Yes
1   2016      Yes
2   2017       No

df['Continent'] = np.random.choice(list(countries), len(df))
df['Country'] = df['Continent'].map(countries)

df

    Year Approved Continent    Country
0   2016      Yes    Africa      Ghana
1   2016      Yes      Asia    Vietnam
2   2017       No    Europe  Lithuania

您从country键列表中随机选择len(df)个键,然后使用country字典作为映射器来查找先前选择的键的对应国家/地区。

关于python - 如何用随机字典值填充 pandas 数据框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464362/

相关文章:

python - sqlalchemy:关闭声明式多态连接?

python - 使用 Pandas [with key column] 将 CSV 与不同的列组合

python - Pandas 相当于 np.where

python - 在 Pandas 中将两个 MultiIndex 级别合并为一个级别

.net - Dictionary<> 在顺序与随机上的表现

Java:Collections.unmodifiedMap 与 Collections.checkedMap

python - 遍历列表的列表列表和元组列表

python - 试图让一个函数返回最长的字符串

python动态创建字典的字典

python - 如何应用适当的阈值以去除二元板上的边缘