I'm looking for a more elegant solution to achieve this with fewer lines of code.Īttached is a sample output JSON for reference. Creating separate code for each of these columns seems redundant and inefficient. # Apply the function to each row of the DataFrameĭf_final = pd.concat(, axis=1)ĭf_final.to_csv('resul.csv', index=False)Īlthough I have successfully created separate columns for some nested data, there are still other nested values within these columns. python - Unable to convert json to csv for a specific columns in pandas - Stack Overflow Unable to convert json to csv for a specific columns in pandas Ask Question Asked today Modified today Viewed 2 times 0 I have a dataset in this form: df. # Custom function to extract values from JSON objects Max_objects = max(len(item_list) for item_list in df_item) You can do this: Read your JSON and write-in a CSV file with importing json and csv modules. # Determine the maximum number of JSON objects in a cell My goal is to get a csv with all the columns inside the json, and to do this I have to create single columns for each nested index inside the JSON.Ĭredentials = " for i in range(max_items) for key in keys] I then get the error: `sequence expected`įirst, your JSON has nested objects, so it normally cannot be directly converted to CSV.I have a nested json output from an API request. I am using Django and the error I received is: `file' object has no attribute 'writerow'` foutopen ('1300 restaurant data.csv', 'a', encoding'utf8') now the rest: for num in range (1,13): f open (str (num)+'.csv',encoding'utf8') for line in f: fout.write (line) f.close () not really needed fout. juanpa. 2 Answers Sorted by: 0 This is my code snippet for merging 13 excel file in to 1 file. But you never even use y, and simply write the same row 7 times (the number of keys). You iterate over it, for y in x: which will iterate over it's keys. I have a JSON file I want to convert to a CSV file. 1 Why do you do this x json.loads (json.dumps (BankData)) In any event, x is a dict object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |