【发布时间】:2018-05-07 22:37:34
【问题描述】:
我是 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 可能会为此提供一个更简单、更受欢迎的解决方案。
【问题讨论】:
标签: python pandas dictionary dataframe random