[英]ValueError: could not broadcast input array from shape (22500,3) into shape (1)
[英]Python - ValueError: could not broadcast input array from shape (5) into shape (2)
我写了一些代码,它接收我的数据帧,其中包含两列 - 一个是字符串,另一个是想法计数 - 代码接收数据帧,尝试几个分隔符并将它与计数交叉引用以检查它正在使用正确的。 我正在寻找的结果是添加一个名为“想法”的新列,其中包含分解的想法列表。 我的代码如下:
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)
有人能告诉我如何解决这个问题吗?
您有 2 个选项与axis=1
一起apply
,您返回单个值或与您的列数长度匹配的长度列表。 如果匹配中的列数将被广播到整行。 如果您返回单个值,它将返回一个熊猫系列
一种解决方法是不使用应用程序。
result = []
for idx, row in df1.iterrows():
result.append(getIdeas(row))
df1['Ideas'] = result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.