william007
william007

Reputation: 18545

Add file if non-existed, otherwise just update the date

Given table as follows:

                    datetime label_file
0 2021-04-16 21:18:01.261623   test.csv

datetime is the current date time and label_file is the file name.

Now if I want to add a new file with current date, if it is an existing file name (say test.csv), I would just update the datetime to latest date time, otherwise I would create a new entry as such:

                    datetime label_file
0 2021-04-16 21:18:01.261623   test.csv
1 2021-04-17 21:18:01.261623  test1.csv

How should I do that?

The code for original table is as follows:

df=pd.DataFrame({"datetime":[pd.datetime.now()],"label_file":["test"]})

Upvotes: 0

Views: 38

Answers (1)

Yefet
Yefet

Reputation: 2086

You can append and make use of drop_duplicates to keep only last in case there is duplicates for label_file

df.append(new_files).drop_duplicates(subset="label_file" , keep="last" , ignore_index =True )

Upvotes: 1

Related Questions