Home > Software engineering >  How can I count the rows of this data set using Python and json?
How can I count the rows of this data set using Python and json?

Time:12-22

I am pursuing a MSc in Data Science and I have the following statement:

Import the file EE_points.json from data folder. Examine the data set and display the names of the columns, the contents of the first 7 rows, and the total number of rows.

I did all the steps correctly except the last.

# My answer

import json
import pandas as pd

# To check the data set I do:

with open('./data/EE_points.json') as f:
  data = json.load(f)
  for query in data:
    print(query)

# To check the names of the columns I do:

keys = query.keys()

print(keys)

# To see the first 7 rows I do:

pd.read_json('./data/EE_points.json').head(7)

# To check the total numbers of rows I do:

length = len(query)

print(length)

The last step, when I have to count the rows printing length I get the number of columns instead of rows.

CodePudding user response:

There are many options depending on how the JSON is structured. I can't tell which would apply in your case, so I will post the 3 of them. Hopefully one will work for you.

Structured List:

# Count the number of rows
row_count = 0
for row in data:
    row_count  = 1

print(f'Number of rows: {row_count}')

If the JSON file is a list of lists:

It's what you did, but for the data.

row_count = len(data)

If the JSON file is a dictionary and assuming that each key in the dictionary represents a row:

row_count = len(data.keys())
  • Related