简体   繁体   English

如何使用 python 和数据框从 csv 文件中读取动态数据

[英]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.谢谢你。

CSV 1 CSV 1 csv文件#1

CSV 2 CSV 2 csv文件#2

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.

相关问题 如何使用python从csv文件格式中读取数据 - How to read data from csv file format by using python 如何在Python中使用API​​从CSV文件读取数据 - How to read data from a csv file using API in Python 如何使用pandas.read_csv将CSV文件中的数据插入数据框? - How can I insert data from a CSV file into a dataframe using pandas.read_csv? Python:从csv文件中读取数据帧列表 - Python: Read list of dataframe from csv file 如何使用 pandas 将度分秒 (DMS) 数据直接从 a.CSV 文件读取到 dataframe 作为字符串? - How to read Degree Minute Seconds (DMS) data directly from a .CSV file using pandas into a dataframe as strings? 如何使用 panda.read_csv 从 python 中的 csv 文件中导入数据? - How to import data from csv file in python using panda.read_csv? 使用python将数据从csv文件读取到word文档作为表格 - read data from csv file to word document as a table using python 在Python中使用Pandas从CSV文件读取特定数据 - Using Pandas to read specific data from a CSV file in Python 从雪花读取大数据并使用python写入csv文件 - Read large data from Snowflake and write into a csv file using python 如何从 python 导出 .csv 文件并使用 pandas DataFrame - How to export .csv file from python and using pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM