Python - How to get a specific value form Pandas DataFrame from CSV file

59 Views Asked by At

I have problem with getting a specific value from DataFrame object.

  1. My CVS file looks like:
domain1.com,1.1.1.1,2023-01-01,description 1
domain2.com,2.2.2.2,2023-02-23,,description 2
  1. Next i read CSV using Pandas and Data Frame object
dataset = pd.read_csv('database\domain\ip.csv', sep = ',', header = None)

#headers 
dataset.columns = ['domain', 'ip', 'data', 'ASN']

#format column to datetime format from datetime library
dataset['data'] = pd.to_datetime(dataset['data'], format="%Y-%m-%d")
  1. Next i want check a specific value if existing in DataFrame
if((dataset["domain"] == 'domain2').any()):
  1. I want get a data and I want compare this date to another:
a = dataset[dataset["domain"] == 'wp.pl.com']['data']
    
if (datetime.now() - a) > timedelta(days = 30):
   #something

But i got error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

If i change to: a.any() or a.any() i got another error:

TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'bool

Anybody know how can i compare two dates?

2

There are 2 best solutions below

2
On

Try before to get both dates as strings and convert them to objects using datetime.strptime()

For example:

date_a = '22-06-2023'
date_b = '23-06-2023'

date_a_object = datetime.strptime(date_a, '%d-%m-%Y')
date_b_object = datetime.strptime(date_b, '%d-%m-%Y')

# Do comparison, for example
if date_b_object > date_a_object:
    # some code
0
On

I think i got solution. It is not perfect, looks aful, but I tested it and it works:

aa = a.to_string().split(" ")
for x in aa:
   if(x.find("-") != -1):
       a = datetime.strptime(x, "%Y-%m-%d")