Menu
Converting Json file to Dataframe Python. Ask Question Asked 1 year, 11 months ago. I'm writing the json file into a csv and then trying to convert this to dataframe on which my models can be applied on. Following is my code. Browse other questions tagged machine-learning python csv json dataframe or ask your own question.
Active8 months ago
I need to convert data from my
csv
file to the one i am gonna use which is .js
. If you can see column
'comment'
does not always have record and I need to keep it that way. Also between data there is amount of tabs I need to set as well.I've a file,i am working on right now but It show's me data in row like :
I am new to python and I have no idea how to define what I need to get.
@@ Edit I managed to do that...
Vadim Kotov5,51577 gold badges3636 silver badges4949 bronze badges
![Python Python](/uploads/1/2/5/8/125834895/838267632.png)
Oskar GarczyńskiOskar Garczyński
5 Answers
![Csv Csv](/uploads/1/2/5/8/125834895/151079970.png)
I think this is your answer, this may not be the best way to do it, but it can gives you the result.
output:
ArunArun1,23111 gold badge1111 silver badges1818 bronze badges
Pandas has both built-in .read_csv() and .to_json(). As an intermediate you get then a dataframe with which you can manipulate the data, including defining an index or data model.
philshemphilshem18.5k55 gold badges3636 silver badges9595 bronze badges
Rather than writing a script, you could try the csvjson command line tool (written in Python):
somewhatoffsomewhatoff63411 gold badge99 silver badges2525 bronze badges
Andrey RusanovAndrey Rusanov2,90922 gold badges2121 silver badges3939 bronze badges
You can minimize your script quite a lot given a few details of python:
The details about why this works:
- Files opened as part of with-statements are closed automatically when the block is 'done'. (The corresponding PEP: https://www.python.org/dev/peps/pep-0343/)
- The list constructor can take Iterators (such as a csv.DictReader) as argument. (DictReader documentation: https://docs.python.org/3.7/library/csv.html#csv.DictReader)
This will create a list in memory with a dict for each row in the csv-file which might be worth keeping in mind if you are handling very large files. Sadly it isn't possible as far as I know to send the iterator directly to the json serializer without modifying the iterator or the serializer.
I'll leave the argument about which solution that is better from a maintenance and a readability perspective as an exercise for the reader.
asheidanasheidan