ValueError:列的长度必须与 pandas 中的键的长度相同

7 python pandas

我下面有 df

    Cost,Reve
    0,3
    4,0
    0,0
    10,10
    4,8

len(df['Cost']) = 300

len(df['Reve']) = 300

Run Code Online (Sandbox Code Playgroud)

我需要划分df['Cost'] / df['Reve']

下面是我的代码

df[['Cost','Reve']] = df[['Cost','Reve']].apply(pd.to_numeric)
Run Code Online (Sandbox Code Playgroud)

我收到错误ValueError: Columns must be same length as key

df['C/R'] = df[['Cost']].div(df['Reve'].values, axis=0)
Run Code Online (Sandbox Code Playgroud)

我收到错误ValueError: Wrong number of items passed 2, placement implies 1

jez*_*ael 11

问题是重复的列名,请验证:

#generate duplicates
df = pd.concat([df, df], axis=1)
print (df)
  Cost Reve Cost Reve
0    0    3    0    3
1    4    0    4    0
2    0    0    0    0
3   10   10   10   10
4    4    8    4    8

df[['Cost','Reve']] = df[['Cost','Reve']].apply(pd.to_numeric)
print (df)
# ValueError: Columns must be same length as key
Run Code Online (Sandbox Code Playgroud)

您可以找到此列名称:

print (df.columns[df.columns.duplicated(keep=False)])
Index(['Cost', 'Reve', 'Cost', 'Reve'], dtype='object')
Run Code Online (Sandbox Code Playgroud)

如果列中的相同值可以通过以下方式删除重复项:

df = df.loc[:, ~df.columns.duplicated()]
df[['Cost','Reve']] = df[['Cost','Reve']].apply(pd.to_numeric)

#simplify division
df['C/R'] = df['Cost'].div(df['Reve'])
print (df)
   Cost  Reve  C/R
0     0     3  0.0
1     4     0  inf
2     0     0  NaN
3    10    10  1.0
4     4     8  0.5
Run Code Online (Sandbox Code Playgroud)