简体   繁体   English

如何在python中检查不同csv文件的两列?

[英]how to check two columns of different csv files in python?

row[1] and i[0] has movie id of two different csv files.How to compare the movie id of different files? row [1]和i [0]具有两个不同的csv文件的影片ID。如何比较不同文件的影片ID?

import csv
total=0
k=0
with open('R\genre_matrix2.csv')as Genre:
        csv_genre=csv.reader(Genre,delimiter=',')  

        with open('ratings1234.csv') as CsvFile:
            csv_rating=csv.reader(CsvFile,delimiter=',')
            for xy in range(1,611):
                for row in csv_rating:
                    if row[0] == xy:
                        i=float(row[2])
                        j=float(row[4])
                        k=i*j

                    for i in csv_genre: 

                        if(row[1] == i[0]):

                            val =  [x * k for x in i]
                            total+=val
                            print(row[0] + "\t" + total)

Good morning! 早上好!

have you thought about using about pandas and the pandas.read_csv -Function? 您是否考虑过使用pandas和pandas.read_csv

The code would look like the following 该代码如下所示

import pandas as pd

df = pd.read_csv('R\genre_matrix2.csv', delimiter=',')
df_2 = pd.read_csv('ratings1234.csv')

# looping through the files
# the following code is just a simple example
for i in range(1, df.shape[0]):
    # select row of first dataframe
    df_row = df.iloc[i, columns_index] 
    # select row of second dataframe
    df_row_2 = df_2.iloc[i, columns_index] 
    # compare
    df_row == df_row_2

This would maybe simplify your code. 这可能会简化您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较python中两个csv文件中的两列 - Compare two columns in two csv files in python 如何使用 Python 读取具有不同列数的 csv 文件 - How to read csv files with different amounts of columns using Python 如何使用 python 将列的内容拆分为 csv 文件中的不同列? - How to split contents of column into different columns in csv files using python? Python/Pandas:比较来自不同长度的不同 CSV 文件的两个字符串列,并查找数据相同的位置 - Python/Pandas: Comparing two string columns from different CSV files that are of different lengths and finding where the data is the same Python csv合并具有不同列的多个文件 - Python csv merge multiple files with different columns Python:比较两个csv文件中的特定列 - Python: Comparing specific columns in two csv files 如何验证两个不同的.csv文件列ID与python匹配? - How to verify that two different .csv files column ids match with python? 如何将来自不同 csv 文件的两列合并到一个 csv 文件中 - How to combine two columns from different csv files to one csv file 需要使用 python 合并两个列数相同但标题不同的 csv 文件 - Need to combine two csv files with same number of columns but different headers using python 如何按关键字比较具有不同列数和行数的两个不同 CSV 文件? - How to compare two different CSV files with different number of columns and rows by keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM