簡體   English   中英

如何在 python 的下一列中寫入 csv 文件

[英]How to write to a csv file in a next column in python

我目前有一個 csv 文件,它有四列

在此處輸入圖像描述

下次我寫入文件時,我想從 E1 寫入。 我已經尋找解決方案,但似乎沒有一個有效。

with open(file_location,"w") as csv_file:
            csv_writer = csv.writer(csv_file)
            csv_writer.writerows(list_of_parameters)

其中 list_of_parameters 是所有四列的 zip。

        list_of_parameters = zip(timestamp_list,request_count_list,error_rate_list,response_time_list)

任何人有任何想法來實現這個? 感謝你的幫助。

Python 庫 Pandas 非常適合這類事情。 這是在 Pandas 中執行此操作的方法:

import pandas as pd

# Read file as a DataFrame
df = pd.read_csv(file_location)

# Create a second DataFrame with your new data,
# you want the dict keys to be your column names
new_data = pd.DataFrame({
    'Timestamp': timestamp_list,
    'Key Request': request_count_list,
    'Failure Rate': error_rate_list,
    'Response': response_time_list
})

# Concatenate the existing and new data
# along the column axis (adding to E1)
df = pd.concat([df, new_data], axis=1)

# Save the combined data
df.to_csv(file_location, index=False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM