Title: Loading A CSV Into Pandas
Slug: pandas_dataframe_importing_csv
Summary: Loading A CSV Into Pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [1]:
import pandas as pd
import numpy as np
In [2]:
raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', ".", 'Milner', 'Cooze'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, ".", "."],
'postTestScore': ["25,000", "94,000", 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
df
Out[2]:
In [3]:
df.to_csv('pandas_dataframe_importing_csv/example.csv')
In [4]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv')
df
Out[4]:
In [5]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', header=None)
df
Out[5]:
In [6]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df
Out[6]:
In [7]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', index_col='UID', names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df
Out[7]:
In [8]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', index_col=['First Name', 'Last Name'], names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score', 'Post-Test Score'])
df
Out[8]:
In [9]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=['.'])
pd.isnull(df)
Out[9]:
In [10]:
sentinels = {'Last Name': ['.', 'NA'], 'Pre-Test Score': ['.']}
In [11]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=sentinels)
df
Out[11]:
In [12]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', na_values=sentinels, skiprows=3)
df
Out[12]:
In [13]:
df = pd.read_csv('pandas_dataframe_importing_csv/example.csv', thousands=',')
df
Out[13]: