如何将数据插入到CSV文件的所需列中?

huangapple go评论56阅读模式
英文:

How do I insert data into a desired column of a csv file?

问题

以下是翻译后的内容:

# 创建一个带有所需列标题的.csv文件,从文件夹中读取日期,然后在这些文件上运行机器学习模型。
# 目标是让我能够查看每天发生多少次检测。
# 我的问题是,无论我如何努力,第二次插入的数据似乎总是粘贴在其他数据下的第一列中。

# 创建 .csv 文件
with open(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", 'w') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    filewriter.writerow(['日期', 'Rotifer 计数'])

# 加载原始图像
org_images_path = fr"/wormbot/{lifespan_number}"
# 为文件建立前缀
file_prefix = "frame"
# 排序图像
img_list = sorted(os.listdir(org_images_path))
# 根据前缀筛选排序后的图像
img_list = [img_name for img_name in img_list if img_name if img_name.startswith(file_prefix)]
# 遍历文件夹中的每个其他文件
for index, img_name in enumerate(img_list):
    if index % 2 == 0:
        # 打开图像
        image_path = os.path.join(org_images_path, img_name)
        # 获取日期和时间
        get_image = os.path.getmtime(image_path)
        # 仅保留日期数字
        date_number = datetime.datetime.fromtimestamp(get_image).day
        # 输出日期数字
        number_day = (date_number)
        # 创建数据框架
        df = pd.DataFrame([number_day])
        # 输入到 .csv 文件
        df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=False, mode='a')

# 遍历文件夹中的所有文件
for image in sorted(os.listdir(output_crop)):
    if image.endswith('.jpg'):
        image_path = os.path.join(output_crop, image)
        img = Image.open(image_path)
        # 在图像上运行模型,设置置信度。
        results = model(img, conf=0.19, verbose=False, max_det=5)
        counts = {}
        for result in results:
            boxes = result.boxes.cpu().numpy()
            for box in boxes:
                cls = int(box.cls[0])
                if not cls in counts.keys():
                    counts[cls] = 1
                else:
                    counts[cls] += 1
            try:
                alive = (str(counts[key]))
            except KeyError:
                alive = ('0')
            # 创建包含当前帧和计数的数据框架
            df = pd.DataFrame([alive])
            # 将数据框架添加到 .csv 文件的下一个空行中,根据需要进行更改。
            df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=False, mode='a')
            # 将 current_frame 增加 1
            current_frame += 1
            # 关闭当前图像
            img.close()
英文:

The code displayed below creates a .csv file with my desired column headers, reads dates from a folder of files and then runs a machine learning model on those files. The goal of this is to allow me to see how many detections occur per day. My issue is that I cannot get the second insertion of data to go into the second column under the header. No matter my efforts it seems to always paste in the first column under the other data.

I have tried the columns(['']) command but I get a KeyError.

    # Creates .csv file.
with open(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['Date','Rotifer Count'])
    # Load Original Images
org_images_path = fr"/wormbot/{lifespan_number}"
# Establish Prefix for Files
file_prefix = "frame"
# Sort Images
img_list = sorted(os.listdir(org_images_path))
# Filter Sorted Images by Prefix
img_list = [img_name for img_name in img_list if img_name if  img_name.startswith(file_prefix)]
# Loop through every other file in the folder
for index, img_name in enumerate(img_list):
if index % 2 ==0:
# Open Image
image_path = os.path.join(org_images_path, img_name)
# Get Date and Time
get_image = os.path.getmtime(image_path)
# Filter to Just Date Number
date_number = datetime.datetime.fromtimestamp(get_image).day
# Output Date Number
number_day = (date_number)
# Create Data Frame
df = pd.DataFrame([number_day])
# Input Into .csv File
df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=False, mode='a')
# Loop through all files in the folder
for image in sorted(os.listdir(output_crop)):
if image.endswith('.jpg'):
image_path = os.path.join(output_crop, image)
img = Image.open(image_path)
# Run Model with however much confidence on image.
results = model(img, conf=.19,verbose=False, max_det=5)
counts = {}
for result in results:
boxes = result.boxes.cpu().numpy()
for box in boxes:
cls = int(box.cls[0])
if not cls in counts.keys():
counts[cls] = 1
else:
counts[cls] += 1
try:
alive = (str(counts[key]))
except KeyError:
alive = ('0')
# creates data frame of current frame and count.
df = pd.DataFrame([alive])
# Add data frame to next open row in csv file, change accordingly.
df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=False, mode='a')
# Adds 1 to current_frame to count.
current_frame +=1
# Close current image
img.close()

答案1

得分: 1

我相信你可以通过创建两个序列,然后将它们合并到你的数据框中,并将它们写入CSV文件来简化这个过程。

以下是一些可能会起作用的代码,但可能错过了你试图实现的要点:

# 加载原始图像
# 日期列表
dates = []
org_images_path = fr"/wormbot/{lifespan_number}"
# 为文件建立前缀
file_prefix = "frame"
# 对图像进行排序
img_list = sorted(os.listdir(org_images_path))
# 根据前缀筛选排序后的图像
img_list = [img_name for img_name in img_list if img_name.startswith(file_prefix)]
# 遍历文件夹中的每个其他文件
for index, img_name in enumerate(img_list):
    if index % 2 == 0:
        # 打开图像
        image_path = os.path.join(org_images_path, img_name)
        # 获取日期和时间
        get_image = os.path.getmtime(image_path)
        # 仅筛选出日期数
        date_number = datetime.datetime.fromtimestamp(get_image).day
        # 输出日期数
        number_day = (date_number)
        # 将number_day添加到日期列表中
        dates.append(number_day)

接下来我们将处理你的计数

# 遍历文件夹中的所有文件
rotifier_counts = []
for image in sorted(os.listdir(output_crop)):
    if image.endswith('.jpg'):
        image_path = os.path.join(output_crop, image)
        img = Image.open(image_path)
        # 对图像以一定的置信度运行模型
        results = model(img, conf=0.19, verbose=False, max_det=5)
        counts = {}
        for result in results:
            boxes = result.boxes.cpu().numpy()
            for box in boxes:
                cls = int(box.cls[0])
                if cls not in counts.keys():
                    counts[cls] = 1
                else:
                    counts[cls] += 1
            try:
                alive = str(counts[key])
                rotifier_counts.append(alive)
            except KeyError:
                alive = '0'
                rotifier_counts.append(alive)
        # 增加当前帧的计数
        current_frame += 1
        # 关闭当前图像
        img.close()

最后我们可以从我们的日期和rotifier_counts列表创建一个系列并创建数据框并写入CSV文件

# 创建日期系列以创建数据框
s1 = pd.Series(dates, name="Dates")
s2 = pd.Series(rotifier_counts, name="Rotifer Counts")
# 创建带有列名的数据框
df = pd.concat([s1, s2], axis=1)
# 将df写入CSV文件
df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=True)
英文:

I believe you could simplify this by creating two series, then merging them into your dataframe and writing them out to CSV.

Something like the below should work, but might have missed the essence of what you're trying to accomplish:

# Load Original Images
# Dates list
dates = []
org_images_path = fr"/wormbot/{lifespan_number}"
# Establish Prefix for Files
file_prefix = "frame"
# Sort Images
img_list = sorted(os.listdir(org_images_path))
# Filter Sorted Images by Prefix
img_list = [img_name for img_name in img_list if img_name if  img_name.startswith(file_prefix)]
# Loop through every other file in the folder
for index, img_name in enumerate(img_list):
if index % 2 ==0:
# Open Image
image_path = os.path.join(org_images_path, img_name)
# Get Date and Time
get_image = os.path.getmtime(image_path)
# Filter to Just Date Number
date_number = datetime.datetime.fromtimestamp(get_image).day
# Output Date Number
number_day = (date_number)
# Append number_day to dates list
dates.append(number_day)

Then we would process your counts next:

# Loop through all files in the folder
rotifier_counts = []
for image in sorted(os.listdir(output_crop)):
if image.endswith('.jpg'):
image_path = os.path.join(output_crop, image)
img = Image.open(image_path)
# Run Model with however much confidence on image.
results = model(img, conf=.19,verbose=False, max_det=5)
counts = {}
for result in results:
boxes = result.boxes.cpu().numpy()
for box in boxes:
cls = int(box.cls[0])
if not cls in counts.keys():
counts[cls] = 1
else:
counts[cls] += 1
try:
alive = (str(counts[key]))
rotifier_counts.append(alive)
except KeyError:
alive = ('0')
rotifier_counts.append(alive)
# Adds 1 to current_frame to count.
current_frame +=1
# Close current image
img.close()

Finally, we could create a series from our dates and rotifier_counts list and stand up the dataframe and write to CSV.

# Create series of dates to create data frame
s1 = pd.Series(dates, name="Dates")
s2 = pd.Series(rotifier_counts, name="Rotifer Counts")
# Create dataframe with column names
df = pd.concat([s1, s2], axis=1)
# Write df out to CSV
df.to_csv(fr"/home/lm/anaconda3/envs/yolov8_rotiferdetect/CSV_files/exp_{lifespan_number}.csv", index=False, header=True)

huangapple
  • 本文由 发表于 2023年7月12日 22:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定