Read data into Dataframe

Posted under » Python Data Analysis on 12 June 2023

From Dataframe intro.

If your data sets are stored in a file, Pandas can load them into a DataFrame.

import pandas as pd

df = pd.read_csv('data.csv')

print(df)

     Duration  Pulse  Maxpulse  Calories
0          60    110       130     409.1
1          60    117       145     479.0
2          60    103       135     340.0
..        ...    ...       ...       ...
167        75    120       150     320.4
168        75    125       150     330.4

[169 rows x 4 columns]

Or if there is a first col and you want to index it.

df = pd.read_csv('data.csv', index_col ="stage")

Take note the the csv must be comma delimited and not ;. It is a good idea to fill the data with NULL so that it can be filled or replaced.

If you want to add new columns to your dataframe.

Big data sets are often stored, or extracted as JSON. From file 'data.json'

import pandas as pd

df = pd.read_json('data.json')

print(df.to_string()) 

Load a Python Dictionary into a DataFrame

data = {
  "Duration":{
    "0":60,
    "1":60,
    "2":60,
    "3":45,
    "4":45,
    "5":60
  },
  "Pulse":{
    "0":110,
    "1":117,
    "2":103,
    "3":109,
    "4":117,
    "5":102
  },
  "Maxpulse":{
    "0":130,
    "1":145,
    "2":135,
    "3":175,
    "4":148,
    "5":127
  },
  "Calories":{
    "0":409,
    "1":479,
    "2":340,
    "3":282,
    "4":406,
    "5":300
  }
}

df = pd.DataFrame(data)

print(df)

cont... Analyzing DataFrames

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data