Jan-09-2023, 03:33 AM
I am trying to convert the string into the date format
Here is my code
Any help would be great!
Here is my code
#!/usr/bin/env python
# make sure to install these packages before running:
# pip install pandas
# pip install sodapy
import pandas as pd
from sodapy import Socrata
import datetime as dt
# Unauthenticated client only works with public data sets. Note 'None'
# in place of application token, and no username or password:
client = Socrata("opendata.maryland.gov", "##KEY##")
# First 2000 results, returned as JSON from API / converted to Python list of
# dictionaries by sodapy.
results = client.get("q4mw-f34p", limit=2000)
# Convert to pandas DataFrame
results_df = pd.DataFrame.from_records(results)
#creating dataframe + adding first field from the originating data frame
cleandata = results_df[['legal_description_line_2_mdp_field_legal2_sdat_field_18']].copy()
#changing column name in data frame
cleandata.rename(columns = {'legal_description_line_2_mdp_field_legal2_sdat_field_18':'address'}, inplace = True)
#copying over columns from originating data frame
cleandata['accountnumber'] = results_df['record_key_account_number_sdat_field_3']
cleandata['housetype'] = results_df['mdp_street_address_type_code_mdp_field_resityp']
cleandata['landuse'] = results_df['land_use_code_mdp_field_lu_desclu_sdat_field_50']
cleandata['exemptclass'] = results_df['exempt_class_mdp_field_exclass_descexcl_sdat_field_49']
cleandata['assessmentyear'] = results_df['assessment_cycle_year_sdat_field_399']
cleandata['currentyeartotalassessment'] = results_df['current_assessment_year_total_phase_in_value_sdat_field_171']
cleandata['owneroccupancycode'] = results_df['record_key_owner_occupancy_code_mdp_field_ooi_sdat_field_6']
cleandata['homesteadcreditqualificationcode'] = results_df['homestead_qualification_code_mdp_field_homqlcod_sdat_field_259']
cleandata['homesteadqualificationdate'] = results_df['homestead_qualification_date_mdp_field_homqldat_sdat_field_260']
cleandata['yearbuilt'] = results_df['c_a_m_a_system_data_year_built_yyyy_mdp_field_yearblt_sdat_field_235']
cleandata['datepurchased'] = results_df['sales_segment_1_transfer_date_yyyy_mm_dd_mdp_field_tradate_sdat_field_89']
cleandata['zoning'] = results_df['zoning_code_mdp_field_zoning_sdat_field_45']
cleandata['assessmentyear'] = [dt.datetime.strptime(x, '%Y')
for x in cleandata['assessmentyear']]
#Saving to CSV
cleandata.to_csv('cleandata.csv')
#printing data frame to screen
cleandataThe output for assessmentyear is 2023-01-01 00:00:00. For this instance I just want it to be 2023. Any help would be great!
