<jupyter_start><jupyter_text>Pandas LibraryFast and relational data. The following are important conventions:<jupyter_code>from pandas import Series, DataFrame
import pandas as pd<jupyter_output><empty_output><jupyter_text>Pandas Data Structure Series and DataFrames. **Series**A Series is a one-dimensional array-like object containing an array of data (of any NumPy data type) and an associated array of data labels, called its **index**. The index is on the left, and the values are on the right.<jupyter_code>s1 = Series([1,2, 3, 4, 5])
s1
s1.index
s2 = Series([1380, 1205, 102, 36, 1100], index=['Faith', 'Addie', 'Peyton', 'Erin', 'Margaret'])
s2
s2.index
s2.values<jupyter_output><empty_output><jupyter_text>Boolean Operations<jupyter_code>s2[s2 > 1000]
s2 * s2
'Faith' in s2<jupyter_output><empty_output><jupyter_text>DataFrame - a spreadsheet like structure*   ordered collection of columns*   row index and column indexOne way to construct a DataFrame is from a Python dictionary:<jupyter_code>data1  = { 'state': ['New Mexico', 'Arizona', 'Utah', 'Colorado' ], 'population': [2.01, 7.28, 3.21, 5.76], 'poverty':[.197, .140, .110, .115]}
data1
states = DataFrame(data1)
states<jupyter_output><empty_output><jupyter_text>The above came with an automatic index. We can specify the index by the following:<jupyter_code>states = DataFrame(data1, index=['NM', 'AZ', 'UT', 'CO'])
states<jupyter_output><empty_output><jupyter_text>Extracing Columns<jupyter_code>states['population']
states[['population', 'poverty']]<jupyter_output><empty_output><jupyter_text>Extracting Rows Using Loc (location)<jupyter_code>states.loc['NM']<jupyter_output><empty_output><jupyter_text>Conditionally<jupyter_code>states[states['population'] < 5]
states[(states['population'] < 5) & (states['poverty'] < .15)]<jupyter_output><empty_output><jupyter_text>Quick departure from Pandas -- Linux commands<jupyter_code>!ls
!pwd
!curl <name of some file> #this shows contents of a file<jupyter_output><empty_output><jupyter_text>reading from a csv file NaN = not a number, setinel for missing data<jupyter_code>athletes = pd.read_csv('https://raw.githubusercontent.com/zacharski/ml-class/master/data/athletesMissingValue.csv')
athletes<jupyter_output><empty_output><jupyter_text>We are using the Name col as our index. We do this by: index_col='Name'<jupyter_code>athletes2 = pd.read_csv('https://raw.githubusercontent.com/zacharski/ml-class/master/data/athletesMissingValue.csv', index_col='Name')
athletes2
athletes.set_index('Name', inplace=True)
athletes<jupyter_output><empty_output><jupyter_text>Describing a DataFrame<jupyter_code>athletes.describe()
athletes.mean()
athletes[athletes['Sport'] == 'Basketball'].mean()
athletes.max()
athletes[['Height', 'Weight']] - athletes.mean()
athletes[['Height', 'Weight']].sub(athletes.mean())<jupyter_output><empty_output><jupyter_text>Sorting<jupyter_code>athletes.sort_index()
athletes.sort_index(ascending=False)
athletes.sort_values('Height')
athletes.sort_values('Height', ascending=False)<jupyter_output><empty_output><jupyter_text>First by height, and then by weight<jupyter_code>athletes.sort_values(['Height', 'Weight'], ascending=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup