【问题标题】:Putting list of value into a dataframe as a row将值列表作为一行放入数据框中
【发布时间】:2021-10-19 03:37:10
【问题描述】:
import numpy as np
import cv2
import os
import mahotas
from skimage.io import imread
from skimage.transform import resize
import pandas as pd
from numpy import *


data_path = "/content/drive/My Drive/ADIP/seperate_ricepests6/seperate_ricepests6/"

image_Humoment = pd.DataFrame()

labels = os.listdir(data_path)
for dirname in labels:
    filepath = os.path.join(data_path, dirname)
    print("Extracting ",dirname," ... ")
    for file in os.listdir(filepath):
        filename = os.path.join(filepath, file)       
        image = cv2.imread(filename)
        image_resized = cv2.resize(image, (300,300))
        image_gray = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY)

        df = pd.DataFrame()

        # HUMoments for shape
        image_hu = cv2.HuMoments(cv2.moments(image_gray)).flatten()--------(1)
        df = image_hu.tolist()

    image_Humoment = image_Humoment.append(df, ignore_index=True)

    print(dirname,"ok!")

3 张图像的结果(所有值进入一列)

               0
0   8.329136e-04
1   3.348686e-10
2   4.307844e-13
3   1.657032e-13
4   1.810802e-26
5  -1.712524e-18
6   4.039908e-26
7   9.305856e-04
8   4.107104e-10
9   1.382715e-13
10  9.868068e-13
11 -3.464484e-25
12 -2.459076e-18
13  1.133319e-25
14  9.058344e-04
15  4.825807e-11
16  2.218821e-15
17  1.050373e-13
18 -1.420554e-27

对于一张图像,我在 image_hu 中得到以下结果(一张图像的六个特征)----(1)

 [1.28177255e-03 6.18380479e-09 5.42714183e-12 8.17649469e-12
 3.47643159e-23 3.83751434e-16 4.19301279e-23]

预期结果

但我无法将它们作为行放入数据框中

例如:一张图片一排第二张图片第二排

【问题讨论】:

    标签: python python-3.x pandas python-2.7


    【解决方案1】:

    如果您确定“在 image_hu 中获得以下结果(一张图像的六个特征)”,则有一个具有 6 列的空数据框,并且在 for 循环内将一行(具有 6 个值)附加到数据框的末尾,例如下面-

    image_Humoment = pd.DataFrame(columns=['feature_1', 'feature_2', 'feature_3','feature_4','feature_5','feature_6'])
    #---- the for loop and other logic
    df = image_hu.tolist() # df has 6 values
    # Now append 
    image_Humoment.loc[len(df)+1] = df
    

    试试这个,然后告诉我。

    我不确定返回值是多少

    image_hu = cv2.HuMoments(cv2.moments(image_gray)).flatten()
    

    但是正如您提到的值是一个列表,或者您通过 df=.tolist() 将值作为 6 个元素的列表,我正在尝试为您完成下一步。

    追加方法

    image_hu= image_hu.tolist() # sample value [1,2,3,4,5,6]
    # convert to dataframe(or pd.series) with same column sequence as image_Humoment 
    a_series = pd.Series(image_hu, index = image_Humoment.columns)
    # Then append
    image_Humoment  = image_Humoment.append(a_series, ignore_index=True)
    

    【讨论】:

    • 追加怎么样...我们是否需要编写追加命令来添加下一行,因为它只存储最后一行
    • 哦,是的,我的错,只需更改 image_Humoment.loc[len(df)+1] = df,或者您也可以使用 appens,但是要附加您需要转换 6 个值的列表进入数据框和所有。编辑答案,检查一次。
    • 我们真的需要首先将值转换为列表吗...我们不能将 image_hu 结果转换为数据帧,然后应用 .loc 函数.. 追加看起来像这样吗: image_Humoment = image_Humoment.append(image_Humoment)
    • 不,附加值必须是数据框(或 pd.Series 对象)并且具有相同的列序列,因此您必须将其转换为 pd.DataFrame 对象,然后才能附加。
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多