[英]Read data of a CSV file to create a new CSV file
I have some data on a CSV file.我有一些关于 CSV 文件的数据。 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.
问题是当我尝试使用原始 CSV 文件的一些信息创建一个新的 CSV 文件时。 I would like to save my
analyzed
info in a new CSV.我想将我
analyzed
信息保存在新的 CSV 中。 I don't know how to use the original info to make a new file.我不知道如何使用原始信息制作新文件。
Data.csv enter image description here Data.csv在此处输入图像描述
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:当涉及到 CSV 文件或类似表格的数据时,您可能想使用
pandas
:
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
.保存您转换后或新创建的
DataFrame
。
For installation run用于安装运行
pip install pandas
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.