I am trying to execute below code wherein I want to insert a key in the dictionary with a particular value on a condition where tos is greater than or less than the value of position key in the dictionary. But in the output I see the else or elif condition executed every time.
def convert_csv_to_dataframe_and_then_convert_csv_data_to_dictionary(tos):
data = pd.read_csv(
r"C:\Users\ppathar\Documents\Ingestion files\L3_Ingestion_scenario_files\own_stakes_detail_eq_update.txt",
sep='|')
#print(data)
recs = data.to_dict(orient="index");
# print(recs)
# print(recs[0]['FACTSET_ENTITY_ID'])
for i in recs:
#pd.Series(recs[i]['FACTSET_ENTITY_ID'],recs[i]['FSYM_ID'],recs[i]['POSITION'])
print(recs[i]['POSITION'])
if int(recs[i]['POSITION'])>tos:
recs[i]['is_valid']=True
elif int(recs[i]['POSITION'])<tos:
recs[i]['is_valid'] = False
print(recs)
convert_csv_to_dataframe_and_then_convert_csv_data_to_dictionary(6508861232)
Below is the output.
{
0: {'FSET_ENTITY_ID': 'PRATIO-A',
'FSYM_ID': 'VVGJM-S',
'POSITION': 650886123,
'INDIRECT_OPTIONS': 760000,
'REPORT_DATE': '2022-11-03',
'SOURCE_CODE': 'A',
'COMMENTS': 'INCLUDES CARL AN CRT. INDIRECT REPRESENTS WARRANTS.',
'CURRENT_FLAG': 1,
'is_valid': False},
1: {'FACTSET_ENTITY_ID': 'PRATIO-B',
'FSM_ID': 'VVG1JM-S',
'POSITION': 650886121,
'INDIRECT_OPTIONS': 0,
'REPORT_DATE': '2022-11-03',
'SOURCE_CODE': 'H',
'COMMENTS': 'RESONA BANK LTD/ID135336(408000)',
'CURRENT_FLAG': 1,
'is_valid': False}
}
I have passed tos as 6508861232 which is greater than 650886123 and the other position key value in the dictionary but the is_valid is not added as True in the dictionary. Am I missing something in the code?
CodePudding user response:
Your code is working fine:
if int(recs[i]['POSITION'])>tos:
recs[i]['is_valid']=True
elif int(recs[i]['POSITION'])<tos:
recs[i]['is_valid'] = False
In both the cases int(recs[i]['POSITION'])<tos
, thus
recs[i]['is_valid'] = False
You might want to change the positions of True and False to get desired behavior as:
if int(recs[i]['POSITION'])>tos:
recs[i]['is_valid'] = False
elif int(recs[i]['POSITION'])<tos:
recs[i]['is_valid'] = True
CodePudding user response:
Both the numbers (values of 'POSITION') in your dataframe have 9 digits. The tos
has 10 digits.
So the output you got is correct.