简体   繁体   中英

TypeError : unsupported operand type(s) for +: 'NoneType' and 'int'

import pandas as pd
import numpy as np
import openpyxl
time = pd.ExcelFile('Block_time_JP1.xlsx')
time.head(3)

output:

Date_time   Station     Pending
0   28-11_15:30     DTK2    36
1   28-11_15:30     DTK2    36
2   28-11_15:30     DTK2    36

Then --

d = [0] 
b=[0]


for i in time.index:
    b[0] = time['Pending'][i]
    j = d
    k= d + b
    for j in k:
        df['Date_time'][j] = time['Date_time'][j]
        df['Station'][j] = time['Station'][j]
        df['Pending'][j] = time['Pending'][j]
    d[0] = j+1

I am getting error in k = d + b line. Does anyone has an idea of solving this problem? Please help me out. Thanks in advance!

Sample example ---

input dataset (2 columns)

a 2
b 3

output dataset (2 columns)

a 2
a 2
b 3
b 3
b 3

I am just trying to execute this thing using the logic inside for loop.

Let's mentally step through your code,

b and d start as [0] , one element lists.

for i in time.index:
    b[0] = time['Pending'][i]   # I'm guessing `b` is now `[36]`
    j = d                       # j is d, not a copy
    k= d + b                    # k is [0,36] - list addition is join
    for j in k:                 # this use of j overrides the previous assignment
        # j is going to be 0, and then 36; 0 may be a valid row index; 36?
        df['Date_time'][j] = time['Date_time'][j]
        df['Station'][j] = time['Station'][j]
        df['Pending'][j] = time['Pending'][j]
    d[0] = j+1           # I expect d to be [37]

I won't continue, but that should give you an idea of what you need to track when doing iterations like this. When you get errors like this, make sure you know what the variable are. The error indicates that some how d became None , and b a number, not a list. It isn't obvious from the code how that happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM