# Start writing code here...
# import pandas as pd
import pandas as pd
# list of strings
lst = ['Dog', 'Rat', 'Turtle']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
print(df)
0
0 Dog
1 Rat
2 Turtle
# This object is something called a dictionary
petDictionary = {'Name':['Boo', 'Billy', 'Bortus'], 'Species': ['Dog', 'Rat', 'Tortois'], 'Age': [5, 2, 3]}
print(petDictionary)
{'Name': ['Boo', 'Billy', 'Bortus'], 'Species': ['Dog', 'Rat', 'Tortois'], 'Age': [5, 2, 3]}
#Now we will make a dataframe from our dictioary
petDataframe = pd.DataFrame(petDictionary)
print(petDataframe)
Name Species Age
0 Boo Dog 5
1 Billy Rat 2
2 Bortus Tortois 3
#A dictionary has things called keys. Let's access them.
for key in petDictionary:
print(key)
Name
Species
Age
#OK time to try and read in a data file ... it will be like one of the geo_info.txt files we use for S/GMR research
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
noPinsdata = pd.read_csv('non_Info07_forPandas.txt', usecols=range(25), delim_whitespace=True) #nondata is a dataframe object
#Note: Above, delim_whitespace=True is needed b/c the csv file has different numbers of spaces between successive values in a row
# The assignment of values into the columns will start to mess up if I don't use this. Thank you stackoverflow.com!
#
#Below are two ways to show the data. print() is less fancy; giving its name produces a cooler view of a dataframe object
#print(noPinsdata)
noPinsdata
%reset
#Lets now try to slice out columns that we desire
phiColumn = noPinsdata["phi"]
print("The column corresponding to Phi is \n", phiColumn)
The column corresponding to Phi is
0 0.838408
1 0.838408
2 0.838408
3 0.838408
4 0.838408
...
996 0.838408
997 0.838408
998 0.838408
999 0.838408
1000 0.838408
Name: phi, Length: 1001, dtype: float64
rattler_counts = noPinsdata["rattler_count"]
seeds = noPinsdata["seed"]
#rattlersAndSeeds = np.column_stack((rattler_counts, seeds))
#The above seems to work, but here is another way to do a two-column numpy array
rattlersAndseeds=np.array(rattler_counts,seeds)
print("The numbers of ratters and the seed are", rattlersAndSeeds)
The numbers of ratters and the seed are [[ 12 90000]
[ 230 90001]
[ 230 90002]
...
[ 10 90998]
[ 230 90999]
[ 8 91000]]
#let's practice accessing elements
justRattlers = rattlersAndSeeds[:,0]
justSeeds = rattlersAndSeeds[:,1]
#print(justRattlers)
print(justSeeds)
plt.plot(justRattlers,justSeeds, 'ro')
[90000 90001 90002 ... 90998 90999 91000]