0

When I print temp_dict , it is something like this,

 {u'part_b_deductible': None, u'off_exchange': True, u'plan_description': None, u'sbc_download_url': None, u'price_note': None, u'starting_percentage_fpl': 0.0, u'is_uhc_plan': False, 'issuer_id': (484,), u'promotional_label': None, u'metal_level_name': u'Silver', u'network_url': None, u'group_or_individual_plan_type': u'Group', u'treatment_cost_calculator_url': None, u'hios_plan_identifier': u'99806CAAUSJ-TMP', u'original_medicare': None, u'part_d_prescription_coverage': None, u'data_sourced_from': u'uhc', u'price_period': u'Monthly', u'is_age_29_plan': False, u'type': u'MetalPlan', u'plan_year': 2018.0, u'plan_detail_footer': None, u'default_bhp': None, u'formulary_url': None, u'plan_detail_items': None, u'highlight_6': None, u'highlight_4': None, u'highlight_5': None, u'hsa_eligible': 0.0, u'highlight_3': u'PCP 20% coinsurance', u'highlight_1': u'Silver', u'name': u'WI 80 INDEMNITY 18 OPTION 1 SILVER RX $10/45/90/25%', u'network_description': None, u'plan_detail_header': None, 'service_area_id': (1,), u'ending_percentage_fpl': 0.0, u'highlight_2': u'Indemnity', u'on_exchange': False, u'network_type': u'Indemnity', u'recommended': None}

But when I copy it in the csv, the data there is -

,data_sourced_from,default_bhp,ending_percentage_fpl,formulary_url,group_or_individual_plan_type,highlight_1,highlight_2,highlight_3,highlight_4,highlight_5,highlight_6,hios_plan_identifier,hsa_eligible,is_age_29_plan,is_uhc_plan,issuer_id,metal_level_name,name,network_description,network_type,network_url,off_exchange,on_exchange,original_medicare,part_b_deductible,part_d_prescription_coverage,plan_description,plan_detail_footer,plan_detail_header,plan_detail_items,plan_year,price_note,price_period,promotional_label,recommended,sbc_download_url,service_area_id,starting_percentage_fpl,treatment_cost_calculator_url,type
0,uhc,,0.0,,Group,Silver,Indemnity,PCP 20% coinsurance,,,,99806CAAUSJ-TMP6,0.0,False,False,484,Silver,WI 80 INDEMNITY 18 OPTION 1 SILVER RX $10/45/90/25%,,Indemnity,,True,False,,,,,,,,2018.0,,Monthly,,,,1,0.0,,MetalPlan

using below code:

  (pd.DataFrame.from_dict(data=temp_dict, orient='columns')
                             .to_csv('dump.csv', header=True))

Why is it starting with a comma ??

Edit -:

Not sure why the downvote to the question. Can this be upvoted pls.

user1896796
  • 731
  • 3
  • 9
  • 25
  • you forgot to pass the argument `index=False` to your csv function. This is a common mistake when you don't care about a numbered index (0,1,2,...). But let us say you had datetime as index, then you would want that to come with. It's a trade-off. – Anton vBR Oct 17 '18 at 06:08
  • Possible duplicate of [How to avoid Python/Pandas creating an index in a saved csv?](https://stackoverflow.com/questions/20845213/how-to-avoid-python-pandas-creating-an-index-in-a-saved-csv) – Anton vBR Oct 17 '18 at 06:10

2 Answers2

2

Use parameter index=False:

pd.DataFrame.from_dict(data=temp_dict, orient='columns')
            .to_csv('dump.csv', header=True, index=False)

If want only header:

pd.DataFrame(columns=df.columns.tolist()).to_csv('dump.csv', index=False)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try this:

df = pd.DataFrame(temp_dict)
df.to_csv('dump.csv', sep=',', index=None)
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58