将变量放入特定位置[行,列]中

2024-06-11 17:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直致力于将字符串变量“revenue”放入pandas数据帧df1。如你所见,我用了df.ait

关于代码的更多细节:它是关于通过m counting循环查找特定的日期行

我的问题发生在.iat

if info[1] == "1": #Get Kikko1
    listofdates = df.Date.tolist() 
    m = 0
    for i in listofdates:
        if i != date: #Counting the rows
            m = m+1
        elif i == date: #Select the row with the matched date
            df.iat[m, 9] = "revenue"
            break

错误显示:

IndexError: index 36 is out of bounds for axis 0 with size 31

Tags: the数据字符串pandasdffordateif
1条回答
网友
1楼 · 发布于 2024-06-11 17:19:47

使用pandas这样的包的一个主要好处是避免了这种手动循环,这种循环很难遵循和修改

我想你可以在一条线上做你想做的事。比如:

df1.loc[date, 9] = 'revenue'

如果这不起作用,你能在你的问题中编辑一些示例数据和你想要的输出吗

相关问题 更多 >