[英]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.