[英]How to read a dynamic data from csv file using python and dataframe
Can someone please assist me in reading the following data coming from csv using pandas?有人可以帮助我使用熊猫阅读来自 csv 的以下数据吗? The labels in the first column may be more/less and the countries could also change.第一列中的标签可能更多/更少,国家也可能发生变化。 How do I iterate through each row and column to read and write these stats to sql provided that labels and countries get appended/removed?如果标签和国家/地区被附加/删除,我如何遍历每一行和每一列以读取这些统计信息并将其写入 sql? Thank you.谢谢你。
You can actually use df.iterrows()您实际上可以使用 df.iterrows()
ex:前任:
for index, rows in df.iterrows():
index # this will give you the index of the particular row
rows['column_name'] #this will give you the row of the particular column.
You can read csv file using您可以使用读取 csv 文件
import pandas as pd
# reading a csv file
df = pd.read_csv('filepath')
You can iterate through dataframe using您可以使用遍历数据框
for index, row in df.iterrows():
# details of the country
print (row['countryone'], row['countrytwo'])
# get the country name
print (row['country_name'])
You can also check the country by iterating through the columns您还可以通过遍历列来检查国家/地区
for col_name in df.columns:
print(col_name)
Iteration through every data in the file can affect the performance of the program and the database.遍历文件中的每个数据都会影响程序和数据库的性能。 So if you are trying to save the data in the dataframe at once you can try this too因此,如果您尝试一次将数据保存在数据框中,您也可以尝试这样做
df.to_sql('short description', connection, if_exists='append', index=False)
connection
is the database connection connection
是数据库连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.