There is information about the dates of birth of employees of the institution.

a) Determine the youngest employee.

b) Determine the most senior employee.

c) Get a list of all employees born in the spring.

my code is:

f=open('1.txt','rt') q=f.read() in_list=list(filter(lambda x: x != '', q.split('\n'))) out_list=[] for string in in_list: item=list(map(int,string.split(' ')[2].split('.'))) out_list.append(item) print(out_list) 

Further, I understand that it is necessary to calculate the difference between today's date and dates of birth of employees -> the greatest number of days in the difference is the senior employee, the smallest is the youngest. But how to implement it? Tell me please ! [enter image description here ] 1

    1 answer 1

    For these purposes, the ideal module Pandas .

    Read the CSV file in the Pandas DataFrame and parse the date:

     import pandas as pd # pip install pandas fn = r'C:\temp\1.txt' df = pd.read_csv(fn, delim_whitespace=True, header=None, parse_dates=['DOB'], names=['FirstName','LastName','DOB']) 

    happened:

     In [55]: df Out[55]: FirstName LastName DOB 0 А Б 2001-06-23 1 В Г 2000-11-13 2 Д Е 2001-09-17 3 Ж 3 2002-04-07 4 И К 2000-12-27 

    Determine the youngest employee

     In [56]: df.nlargest(1, 'DOB') Out[56]: FirstName LastName DOB 3 Ж 3 2002-04-07 

    Determine the most senior employee

     In [57]: df.nsmallest(1, 'DOB') Out[57]: FirstName LastName DOB 1 В Г 2000-11-13 

    Get a list of all employees born in the spring.

     In [58]: df[df['DOB'].dt.month.between(3, 5)] Out[58]: FirstName LastName DOB 3 Ж 3 2002-04-07 

    To install Pandas (using pip ):

     pip install pandas 

    or with the help of conda (package manager from the Anaconda distribution):

     conda install pandas 
    • ModuleNotFoundError: No module named 'pandas' how to be? - Imao
    • one
      @Imao, supplemented the answer ... - MaxU
    • can I contact you in messengers for example? - Imao
    • @Imao, I'm not morally ready for such close communication ;-) - MaxU
    • I have questions about this problem. Can I voice them here? - Imao