简体   繁体   中英

Read data of a CSV file to create a new CSV file

I have some data on a CSV file. As you can see in the code, I can read the file and print the info I need. The problem is when I try to create a new CSV file with some info of Original CSV file. I would like to save my analyzed info in a new CSV. I don't know how to use the original info to make a new file.

Data.csv enter image description here

import csv

with open('Data.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        analyzed = (row[0],row[3],row[3]<0.25)
        print(analyzed)

You probably want to use pandas when it comes to CSV files or table-like data:

import pandas as pd

df_data = pd.DataFrame.from_csv('Data.csv')

# Analyze
for index, row in df_data.iterrows():
    pass

df_data.to_csv('new_Data.csv')

For reading you have several options like

and, as you see, use

to save your transformed or newly created DataFrame .

For installation run

pip install pandas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM