Search code examples
pythonpandasindex-error

Place Variable into a Specific Location [row,column] Pandas Python


I've been working to place a string variable "revenue" into a pandas dataframe df1. As you can see, I used df.ait.

More details about the code: It's about finding the specific date row, by m counting loop.

My issue occurs at the .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

The error says:

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

Solution

  • One of the main benefits of using a package like pandas is to avoid this kind of manual looping, which is very difficult to follow and modify.

    I think you can do what you need to in one line. Something like:

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

    If that doesn't work, could you edit into your question some example data and your desired output?