Home > Software engineering >  Compare unique identifiers, if true, do action in python Pandas
Compare unique identifiers, if true, do action in python Pandas

Time:04-25

I have 2 files that contain partly the same items. To detect them there exists a unique identifier (UID).

What I try to achieve is to compare the UIDs in the first file, and compare them to the UIDs in the second file. If those are identical, another column in the first file should be filled with content in the second file of the respective column.

import pandas as pd

dfFile2 = pd.read_csv("File2.csv", sep=";")
dfFile1 = pd.read_csv("File1.csv", sep=";")

UIDURLS = dfFile2["UID"]
UIDKonf = dfFile1["UID"]

URLSurl = dfUrls["URL"]
URLSKonf = dfKonf["URL"]


for i in range(0, len(UIDKonf)):
    for j in range(0, len(UIDURLS)):
        if UIDKonf.at[i] == UIDURLS.at[j]:
            URLSKonf.at[i] = URLSurl[j]

The code above does not give me any errors, but I also want it to directly write into the original .csv and not into the Dataframe. How could I achieve that?

Best

CodePudding user response:

If you create the DataFrame with the updated information you want, you can write it back to a csv in pandas using DataFrame.to_csv

  • Related