idek what chapter Flashcards
Series vs. DataFrames
series= 1d arrays
dataframes = 2d arrays
each of which have rows and columns which can be labelled
how to create series or dataframe
use python dictionary
define a dictionary
structure but mapping type (not a data sequence, so is indexed using keys)
is of type dict
mutable!
how to create a dictionary?
key-value pairs separated by commas, put in CURLY BRACKETS
bigguy = {‘keyname’:value_inside_key, ‘biggestguy’:’masonReilly}
how to index in a dictionary
dictionary[‘keyname’]
error if keyname DNE or index using integers
dictionary = mutable or not?
mutable (including modifying things inside it)
how to add/remove values to dictionary
dictionary[‘newkeyname’] = 123453241
del dictionary[‘nameofthingimremoving’]
len(mydictionary) returns?
amnt of key pairs
list(mydictionary) returns?
‘year’ in mydictionary returns?
returns the list equivalent
returns whether or not a key of that name exists in the dictionary
mydictionary = {‘year’:2313, ‘big’:’mason reilly’}
mydictionary.get(‘size’)
mydictionary.get(‘size’, -999)
mydictionary.get(‘big’}
what will these two getters return?
- None
- -999
- ‘mason reilly’
mydictionary.get()
mydictionary.get(‘keyname’)
mydictionary.get(‘keyname’, thing if keyname is not in dictionary)
why is get better than indexing a keyname?
indexing will return an error, get will return a value
dictionary pop
mydictionary.pop(‘name’)
mydictionary.pop(‘name’, thing if name DNE)
removes and returns removed value
error if no thing for DNE is specified and name DNE
dictionary popitem
mydict.popitem()
removes and returns last key of dictionary as a tuple
dictionary clear
mydict.clear()
clears all key-pairs from dictionary
keys
values
items
mydict.keys()
mydict.values()
mydict.items()
all used to iterate, returns VIEW OBJECTS of the certain things
keys returns a view object list of the names
values returns values list
items returns keys and values respectively, need 2 iterators to work with them
how to store multiple dictionaries
list of dictionaries
can you have data sequences inside of dictionaries?
duhh
how would you do a dictionary comprehension?
mydict = {i:i**3 for i in range(5)}
each of these adds a new key-value pair
do key names have to be strings?
nah
should you import pandas with numpy? why?
yes, bcz its built on nump-y
What is a Pandas Series?
1d array created using a list or array
Stored as a NumPy array, can index it and slice it just like every other array
what does this return:
numseries = pd.Series([11, 2, 3, 5])
numseries
0 11
1 2
2 3
3 5
dtype: int64
indices, actual data passed, AND dtype are stored in variable!
how to access just the numbers in Series?
numseries.values
returns
array([11, 2, 3, 5])
values
numseries.values
array([11, 2, 3, 5])
returns the non-index, non type, actual values stored in the Series.
can you use index on a series?
yes, but itll just give you the start index the end index and the steps between each index
create a series with indices a, b, c, d
mystuff = pd.Series([1, 2, 3, 4], [‘a’, ‘b’, ‘c’, ‘d’])
these new indices can be used to index and slice into the series
numlabels[‘b’:’d’] returns
b 2
c 3
d 4
dtype int64
do specified indices work inclusively on all or like normal python?
inclusively on all, only the OG default integer indice slicing does not include the last index
can you make a series into a dictionary?
yes!
typecast whatever dictionary you want and youll get the indexes as the names and the values as the values with the little dtype at the bottom
wtf is a DataFrame
2d array with labels for rows and columns
make a dataframe
dataf = pd.DataFrame(myseries, columns = [‘name of first column’])