乐闻世界logo
搜索文章和话题

How to add pandas data to an existing csv file?

1个答案

1

When using the Pandas library to append data to an existing CSV file, we typically use the to_csv method with the mode='a' parameter to append data. The specific steps are as follows:

  1. Import the Pandas library: First, ensure you have installed the pandas library and imported it into your script.

    python
    import pandas as pd
  2. Create or specify a DataFrame: You need a DataFrame containing the data you want to append to the CSV file. This DataFrame can be newly created or read from another data source.

    python
    data = {'Name': ['John Doe', 'Jane Smith'], 'Age': [28, 34]} df = pd.DataFrame(data)
  3. Use the to_csv method to append data: Use the to_csv method with mode='a' (append mode) and header=False (if you don't want to write column headers each time) to append data to the existing CSV file.

    python
    df.to_csv('existing_file.csv', mode='a', header=False, index=False)
    • mode='a': Ensures data is appended to the end of the file rather than overwriting existing data.
    • header=False: Prevents writing column headers again if the CSV file already includes them.
    • index=False: Avoids writing the DataFrame's index to the CSV file.

Example

Suppose we already have an employees.csv file containing employee names and ages. Now, we have new employee data as follows:

python
new_data = {'Name': ['Alice Brown'], 'Age': [30]} new_df = pd.DataFrame(new_data)

We want to append this new data to the employees.csv file. The operation is as follows:

python
new_df.to_csv('employees.csv', mode='a', header=False, index=False)

After this, the employees.csv file will contain the original data along with the new employee data for Alice Brown.

Using this method, we can efficiently append data to an existing CSV file without rewriting the entire file each time, which is particularly useful when handling large datasets.

2024年7月20日 14:45 回复

你的答案