Advertisement
VssA

new db

Dec 22nd, 2023
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import pandas as pd
  2. import sqlite3
  3.  
  4. database_name = input()
  5. csv_file = input()
  6. table_name = input()
  7. currency_table = input()
  8.  
  9. def preprocess_vacancies_data(database_name, csv_file_name, table_name, currency_table):
  10.     df_vacancies = pd.read_csv(csv_file_name)
  11.  
  12.     conn = sqlite3.connect(database_name)
  13.  
  14.  
  15.     df_vacancies['salary_from'] = df_vacancies.apply(lambda row: convert_currency(row['salary_from'], row['salary_currency'],row['published_at'],database_name), axis=1)
  16.  
  17.     df_vacancies['salary_to'] = df_vacancies.apply(lambda row: convert_currency(row['salary_to'], row['salary_currency'],row['published_at'],database_name), axis=1)
  18.  
  19.     df_vacancies['salary'] = df_vacancies.apply(lambda row: calculate_average_salary(row['salary_from'], row['salary_to']), axis=1)
  20.  
  21.     df_vacancies = df_vacancies[['name', 'salary', 'area_name', 'published_at']]
  22.  
  23.     df_vacancies.to_sql(table_name, conn, if_exists='replace', index=False)
  24.  
  25.     conn.close()
  26.    
  27. def convert_currency(value, currency, year,database_name):
  28.     if pd.isnull(value) or pd.isnull(currency):
  29.         return None
  30.  
  31.     coefficient = get_currency_coefficient(currency_table, currency, year,database_name)
  32.  
  33.     if coefficient is not None:
  34.         return round(value * coefficient)
  35.     else:
  36.         return None
  37.    
  38. def get_currency_coefficient(currency_table, currency, date,database_name):
  39.     if currency == 'RUR':
  40.         return 1
  41.     if currency is not None and currency != 'RUR':
  42.         conn = sqlite3.connect(database_name)
  43.         cursor = conn.cursor()
  44.         query = f"SELECT {currency} FROM {currency_table} WHERE date = '{date.split('T')[0][0:7]}'"
  45.         cursor.execute(query)
  46.         coefficient = cursor.fetchone()[0]
  47.         conn.close()
  48.         return coefficient
  49.     else:
  50.         return None
  51.  
  52.    
  53. def calculate_average_salary(salary_from, salary_to):
  54.     if pd.isnull(salary_from) and pd.isnull(salary_to):
  55.         return None
  56.     elif pd.isnull(salary_from):
  57.         return salary_to
  58.     elif pd.isnull(salary_to):
  59.         return salary_from
  60.     else:
  61.         return (salary_from + salary_to) // 2
  62.    
  63.  
  64. preprocess_vacancies_data(database_name, csv_file, table_name, currency_table)
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement