我是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可能提供了更简单、更“pandorable”的解决方案。
我肯定可以使用for循环或while循环来填充df ['Continent']和df ['Country'],但我觉得使用.apply()和np.random.choice可能会提供更简单且更符合Pandas风格的解决方案。