ValueError:在DataFrame中创建列期间,值的长度与索引的长度不匹配

2024-04-26 15:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

试图将所有计数的值放入数据框,并希望另存为csv文件,但出现错误(ValueError:值的长度与列中的索引长度不匹配),即使长度相同

import cv2
from skimage.io import imread, imshow
from skimage.transform import resize
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
from tqdm import tqdm
import pandas as pd
import seaborn as sns

################################################################
path = "C:\pfm\seg/"

ids = os.listdir(path)
#print(ids)

counts_list = []
df =pd.DataFrame()

for n, id_ in tqdm(enumerate(ids), total=len(ids)):

    
    df = pd.read_excel(path+id_)
    print(path+id_)

    df.head(1)

    plt.hist(df['Area'],bins=15)
    counts, bins, bars = plt.hist(df['Area'],bins=15)
    print(counts)
    

    #sns.distplot(df['Area'], kde=True, bins=100, color='red')
    #sns.pairplot(df.loc[:,['Area']])
    
    counts_list.append(counts)

    
    df['counts'] = counts_list

    
    df.to_csv('C:\pfm\seg/counts.csv')

    plt.xlabel('Pearlite Area')
    plt.title(ids[n]+'Pearlite Area distriburion')
    
    plt.savefig(ids[n]+'.jpg')

    plt.show()

Tags: csvpathfromimportidsdfasplt
1条回答
网友
1楼 · 发布于 2024-04-26 15:15:31

您最好提供示例数据。我从https://datatofish.com/plot-histogram-python/取一个修改过的例子

import matplotlib.pyplot as plt
import pandas as pd

x = [1,1,2,3,3,5,7,8,9,10]

plt.style.use('ggplot')
counts, bins, bars = plt.hist(x, bins=10)
# counts is now array([2., 1., 2., 0., 1., 0., 1., 1., 1., 1.])

从计数返回一个数组,并将其添加到嵌套列表“计数列表”中

counts_list = []
for i in range(10):
    counts_list.append(counts)
    

生成一些数据:

df_x = pd.DataFrame([x]*10)

如果每个计数项需要一列,则在将子元素放入df之前,可以将其再次拆分为列

df_with0countsColumns = pd.DataFrame([counts]*10, columns=[a for a in range(min(x), max(x)+1)])
df_no0countsColumns = pd.DataFrame([[int(a) for a in counts if a in x]]*10, columns=[a for a in range(min(x), max(x)+1) if a in x])
df = pd.concat([df_x.add_suffix('_x'), df_no0countsColumns.add_prefix('countsOf_')], axis=1) 

结果:

   0_x  1_x  2_x  3_x  4_x  ...  counts_5  counts_7  counts_8  counts_9  counts_10
0    1    1    2    3    3  ...       1.0       1.0       1.0       1.0        1.0
1    1    1    2    3    3  ...       1.0       1.0       1.0       1.0        1.0
2    1    1    2    3    3  ...       1.0       1.0       1.0       1.0        1.0
...

或者,如果只需要一列,所有计数都作为字符串:

df = df_x.add_suffix('_x')
df['counts'] = [','.join(str(int(y)) for y in x) for x in counts_list]

结果:

   0_x  1_x  2_x  3_x  4_x  5_x  6_x  7_x  8_x  9_x               counts
0    1    1    2    3    3    5    7    8    9   10  2,1,2,0,1,0,1,1,1,1
1    1    1    2    3    3    5    7    8    9   10  2,1,2,0,1,0,1,1,1,1
2    1    1    2    3    3    5    7    8    9   10  2,1,2,0,1,0,1,1,1,1

相关问题 更多 >