pandas TypeError:不支援+的算子类型:'NoneType'和'int'

xyhw6mcr  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(135)
import pandas as pd
import numpy as np
import openpyxl
time = pd.ExcelFile('Block_time_JP1.xlsx')
time.head(3)

输出:

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

然后--

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

我在k = d + b行中遇到错误。有人有解决这个问题的方法吗?请帮助我解决这个问题。提前感谢!
示例---
输入数据集(2列)

a 2
b 3

输出数据集(2列)

a 2
a 2
b 3
b 3
b 3

我只是尝试使用for循环中的逻辑来执行这个东西。

3df52oht

3df52oht1#

让我们在头脑中逐步浏览您的代码,
bd[0]开头,其中一个元素列出。

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]

我不会继续,但这应该会给予你知道在做这样的迭代时需要跟踪什么。当你遇到这样的错误时,确保你知道变量是什么。错误表明d是如何变成None的,b是一个数字,而不是列表。从代码中看不出这是如何发生的。

相关问题