📜  如何修复:值的长度与索引的长度不匹配

📅  最后修改于: 2022-05-13 01:55:36.013000             🧑  作者: Mango

如何修复:值的长度与索引的长度不匹配

在本文中,我们将修复错误:值的长度与Python中索引的长度不匹配。

出现此错误的案例:

Python3
# importing pandas
import pandas as pd
  
sepal_length = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 
                4.6, 5.0, 4.4, 4.9]
sepal_width = [4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 
               4.9, 5.1, 5.2, 5.3]
petal_length = [3.3, 4.6, 4.7, 5.6, 6.7, 5.0, 4.8]
petal_width = [3.6, 5.6, 5.4, 4.6, 4.4, 5.0, 4.9]
  
# DataFrame with 2 columns
df = pd.DataFrame({'sepal_length(cm)': sepal_length,
                   'sepal_width(cm)': sepal_width})
  
df['petal_length(cm)'] = petal_length
df['petal_width(cm)'] = petal_width
  
print(df)


Python3
# importing pandas
import pandas as pd
  
# importing numpy
import numpy as np
sepal_length = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4,
                4.6, 5.0, 4.4, 4.9]
sepal_width = [4.6, 5.0, 5.4, 4.6, 5.0, 4.4,
               4.9, 5.1, 5.2, 5.3]
petal_length = [3.3, 4.6, 4.7, 5.6, 6.7, 5.0, 4.8]
  
# numpy array of length 7
petal_width = np.array([3.6, 5.6, 5.4, 4.6, 4.4, 
                        5.0, 4.9])
  
# DataFrame with 2 columns of length 10
df = pd.DataFrame({'sepal_length(cm)': sepal_length,
                   'sepal_width(cm)': sepal_width})
  
# Adding list to pandas DataFrame
df['petal_length(cm)'] = pd.Series(petal_length)
  
# Adding numpy array to pandas DataFrame
df['petal_width(cm)'] = pd.Series(petal_width)
  
print(df)


输出:

ValueError: Length of values (7) does not match length of index (10)

错误原因:

在这种情况下,pandas DataFrame 的索引长度(即当前 DataFrame 的列的长度)为 10,不等于在这种情况下为 7 的新列表或 NumPy 数组的长度。

pd.Index.size!=len(petal_width)

修复错误:

可以通过使用 pandas Series()函数预处理将成为 DataFrame 列的新列表或 NumPy 数组来修复此错误,该函数实际上通过添加 NaN 将列表或 NumPy 数组转换为 DataFrame 列长度的大小如果列表或 NumPy 数组的长度较小,否则如果列表或 NumPy 的长度较大,则它采用具有 pandas 数据帧列长度的列表或 NumPy 数组。

列表的语法:

将 list1 视为Python列表

df['new_column'] = pd.Series(list1)

NumPy 数组的语法:

将 numarr 视为一个 numpy 数组

df['new_column'] = pd.Series(numarr)

Python3

# importing pandas
import pandas as pd
  
# importing numpy
import numpy as np
sepal_length = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4,
                4.6, 5.0, 4.4, 4.9]
sepal_width = [4.6, 5.0, 5.4, 4.6, 5.0, 4.4,
               4.9, 5.1, 5.2, 5.3]
petal_length = [3.3, 4.6, 4.7, 5.6, 6.7, 5.0, 4.8]
  
# numpy array of length 7
petal_width = np.array([3.6, 5.6, 5.4, 4.6, 4.4, 
                        5.0, 4.9])
  
# DataFrame with 2 columns of length 10
df = pd.DataFrame({'sepal_length(cm)': sepal_length,
                   'sepal_width(cm)': sepal_width})
  
# Adding list to pandas DataFrame
df['petal_length(cm)'] = pd.Series(petal_length)
  
# Adding numpy array to pandas DataFrame
df['petal_width(cm)'] = pd.Series(petal_width)
  
print(df)

输出: