gpt4 book ai didi

Python-值错误: could not broadcast input array from shape (5) into shape (2)

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:01 26 4
gpt4 key购买 nike

我编写了一些代码,它接受我的数据帧,该数据帧由两列组成 - 一列是字符串,另一列是想法计数 - 代码接受数据帧,尝试几个分隔符并将其与要检查的计数交叉引用它正在使用正确的。我正在寻找的结果是添加一个名为“想法”的新列,其中包含分解的想法列表。我的代码如下:

def getIdeas(row):
s = str(row[0])
ic = row[1]
# Try to break on lines ";;"
my_dels = [";;", ";", ",", "\\", "//"]

for d in my_dels:
ideas = s.split(d)
if len(ideas) == ic:
return ideas
# Try to break on numbers "N)"
ideas = re.split(r'[0-9]\)', s)
if len(ideas) == ic:
return ideas
ideas = []
return ideas

# k = getIdeas(str_contents3, idea_count3)

xl = pd.ExcelFile("data/Total Dataset.xlsx")
df = xl.parse("Sheet3")

df1 = df.iloc[:,1:3]

df1 = df1.loc[df1.iloc[:,1] != 0]
df1["Ideas"] = df1.apply(getIdeas, axis=1)

当我运行这个时,我收到一个错误

ValueError: could not broadcast input array from shape (5) into shape (2)

有人可以告诉我如何解决这个问题吗?

最佳答案

对于 applyaxis=1,您有 2 个选项,要么返回单个值,要么返回与列数长度匹配的长度列表。如果匹配的列数将被广播到整行。如果您返回单个值,它将返回 pandas Series

一种解决方法是不使用 apply。

result = []
for idx, row in df1.iterrows():
result.append(getIdeas(row))
df1['Ideas'] = result

关于Python-值错误: could not broadcast input array from shape (5) into shape (2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54362696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com