【发布时间】:2022-01-24 00:49:01
【问题描述】:
我正在尝试在 python 中创建一个函数,该函数将给定列的前 10 个 ID 作为字符串返回,该字符串将成为新列的值。例如,如果前 10 个 id 为 [1,2,3,4,5,6,7,8,9,10],则输出应为“1 2 3 4 5 6 7 8 9 10”。当我应用我拥有的函数时,它只返回空白值。
这是我目前的功能:
def top_ten(month):
top_funds = ""
top_ten = np.array(HF_2018[HF_2018['month']==month-1].nlargest(10, 'Performance')['Fund_ID'])
for i in top_ten:
top_funds += str(i)
return top_funds
这是我尝试创建新列的方式:
HF_2018['top_10'] = top_ten(HF_2018['month'])
任何帮助都会很棒。谢谢!
【问题讨论】:
标签: python pandas function numpy