Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
176 views1 page

Pandas CSV Import and Manipulation

The document shows code for importing pandas and creating a DataFrame from raw dictionary data. It saves the DataFrame to a CSV file, then reads it back in using various pandas functions - printing the DataFrame with default settings, custom column names, index, NA handling, and row skipping. The code demonstrates common workflows for working with DataFrames and CSV files in pandas.

Uploaded by

Prerna Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views1 page

Pandas CSV Import and Manipulation

The document shows code for importing pandas and creating a DataFrame from raw dictionary data. It saves the DataFrame to a CSV file, then reads it back in using various pandas functions - printing the DataFrame with default settings, custom column names, index, NA handling, and row skipping. The code demonstrates common workflows for working with DataFrames and CSV files in pandas.

Uploaded by

Prerna Bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import pandas as pd

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'])
print(df)

import pandas as pd
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.to_csv('project.csv')

import pandas as pd
df = pd.read_csv('project.csv')
print(df)

import pandas as pd
df = pd.read_csv('project.csv', header=None)
print(df)

import pandas as pd
df = pd.read_csv('project.csv', index_col=['First Name', 'Last Name'],
names=['UID', 'First Name', 'Last Name', 'Age', 'Pre-Test Score',
'Post-Test Score'])
print(df)

import pandas as pd
df = pd.read_csv('project.csv', na_values=['.'])
print(pd.isnull(df))

import pandas as pd
df = pd.read_csv('project.csv', skiprows=3)
print(df)

You might also like