read Json file using Pandas - python

I am trying to read a json file using pandas's read_json function and i am getting result but not what i want
My result have first row as a header (Titles) and i want to ignore first row in my result.
Below is my python code.
import json
import pandas as pd
result=pd.read_json('dummy_DB_clean.json')
print result
I tried pandas's json_normalize() function but did not get desired output.
If anyone of you , come across with this problem, please suggest me the solution.
Thanks,

Try this:
import json
import pandas as pd
df=pd.read_json('dummy_DB_clean.json')
df.drop(df.head(1).index, inplace=True)
print df

Related

why i cant load json file to pandas data frame

My code:
import json
import requests
responseGBP=requests.get("https://public.opendatasoft.com/api/records/1.0/search/?dataset=euro-exchange-rates&sort=date&facet=currency&rows=30&facet=date&q=date:[2020-12-01+TO+2020-12-31]&refine.currency=GBP")
response_jGBP=responseGBP.content.decode("utf-8")
df = pd.read_json(response_jGBP)
df
I'm getting this error:
All arrays must be of the same length
I want to get the currency data , but i cant covert the json file to pandas dataframe.
I'm getting "All arrays must be of the same length" Error
Because you didn't analyze dictionary structure properly.
You have to understand that for all columns in pandas rows will be of same count, and you can also load specific part of response if you want.
import pandas as pd
res = requests.get("https://public.opendatasoft.com/api/records/1.0/search/?dataset=euro-exchange-rates&sort=date&facet=currency&rows=30&facet=date&q=date:[2020-12-01+TO+2020-12-31]&refine.currency=GBP")
df = pd.DataFrame(res.json()["records"])
Above code will load the data but not sure if that is you wanted, but you got the idea you have to look at first JSON structure. If needed, you have to modify it as well to load as data frame.

How to convert nested JSON data from a URL into a Pandas dataframe

import json
import pandas
import requests
Convert to Pandas
I know what you're going to say, this has been asked before. But ive gone through a number of posts already and they all require importing the json file into the code already.
So with this code I've been trying to import the json data through a URL, so there is no need to save any files before hand.
Is it even possible?
Please help.
Pandas json_normalize can do just that. Here is an example for which you will have to modify to meet your specific needs:
df = pd.json_normalize(packages_json, record_path='results')
(I omitted the output from the DF because is it unwieldy)

Problem when importing Excel File with Pandas

I'm new to python and was hoping someone could help me out.
I imported an excel file using pandas just to play around with. However when I try do any additional analysis or coding on the data it is only using the header row of the excel file.
Here's one of the codes I used:
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
data=list(df)
print(data)
Here's the output:
runfile('C:/Users/at0789/.spyder-py3/temp.py', wdir='C:/Users/at0789/.spyder-py3')
['Name', 'Number', 'Color', 'Date']
This is what my test file looks like:
you can pass only the string 'C:\Users\at0789\Documents\Test File.xlsx'
And you don't have to print the df, only call it, like that
import pandas as pd
df = pd.read_excel('C:\Users\at0789\Documents\Test File.xlsx')
df
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
df - data-frame
Data-frame have some many built-in function. With optimisation code with less line of code and high performance
One best feature is play example play with data as like sql query

Columns names issues using pandas.read_csv

I am pretty new to python.
I am trying to import the SMSSpam Collection Data using pandas read_csv module.
I
The import went went.
But as the file does not have header I tried to include columns names(variables names : "status" and "message" and ended up with empty file.
Here is my code:
import numpy as np
import pandas as pd
file_loc="C:\Users\User\Documents\JP\SMSCollection.txt"
df=pd.read_csv(file_loc,sep='\t')
The above code works well I got the I got the 5571 rows x 2 columns].
But when I add columns using the following line of code
df.columns=["status","message"]
I ended up with an empty df
Any help on this ?
Thanks
You could try to set the column names at read time:
df=pd.read_csv(file_loc,sep='\t',header=None,names=["status","message"])

searching a csv file and then outputting a term associating to the term

I am creating a code, that read a csvs file and search for a specific item code and then it will output the name of the item.
How would i do this?
I don't have any code yet
Thanks
You can use pandas
Install it, then try
import pandas as pd
df = pd.read_csv('yourFile.csv')
print df

Categories