<jupyter_start><jupyter_text>Lesson on Pandas 25 October 2017<jupyter_code>import pandas as pd
surveys_df = pd.read_csv("surveys.csv")
type(surveys_df)
a = 67
type(a)
surveys_df.dtypes
column_names = list(surveys_df.columns)
column_names
surveys_df.head(6)
surveys_df.shape
pd.unique(surveys_df['species_id'])
plot_names = pd.unique(surveys_df['plot_id'])
print(len(plot_names))
surveys_df['plot_id'].nunique()
surveys_df['weight'].describe()
surveys_df['weight'].count()
sorted_data = surveys_df.groupby('sex')
sorted_data.describe()
sorted_data.mean()
a = ['plot_id','sex']
sorted_data2 = surveys_df.groupby(a)
sorted_data2.mean()
by_plot = surveys_df.groupby('plot_id')
by_plot['weight'].describe()
species_counts = surveys_df.groupby('species_id')['record_id'].count()
species_counts
a = ["plot_id",'sex']
a
surveys_df
surveys_df.groupby('species_id')
species_counts = surveys_df.groupby('species_id')['record_id'].count()
species_counts
double_weight = surveys_df['weight'] * 2
double_weight
double_weight.describe()
surveys_df['weight'].describe()
%matplotlib inline
species_counts.plot(kind='bar')
total_count = surveys_df.groupby('plot_id')['record_id'].nunique()
total_count.plot(kind='bar')
ave_weight = surveys_df.groupby('plot_id')['weight'].mean()
ave_weight.plot(kind='bar')
sex_cout = surveys_df.groupby('sex')['record_id'].count()
sex_cout.plot(kind='bar')
by_plot_sex = surveys_df.groupby(['plot_id','sex'])
plot_sex_count = by_plot_sex['weight'].sum()
plot_sex_count
plot_sex_count.unstack()
plot_sex_count
spc = plot_sex_count.unstack()
s_plot = spc.plot(kind='bar',stacked=True,title="Total weight by plot and sex")
s_plot.set_ylabel("Weight")
s_plot.set_xlabel("Plot")
surveys_df['species_id']
surveys_df.species_id
surveys_species = surveys_df['species_id']
surveys_df[['species_id', 'plot_id']]
surveys_df[['plot_id', 'species_id']]
surveys_df[0:3]
a = list(range(10))
a
a[0:5]
a[4:]
a[0:]
a[:5]
a[-1]
a[-2]
a[::3]
for i in a[::3]:
    print(i)
surveys_df[20:60:5][['plot_id','species_id','sex']]
surveys_df['age'] = 'NaN'
surveys_df
surveys_copy = surveys_df
surveys_copy[0:3] = 0
surveys_copy
surveys_df
surveys_df = pd.read_csv("surveys.csv")
surveys_copy = surveys_df.copy()
surveys_copy[0:3] = 0
surveys_copy.head(6)
surveys_df.head(6)
surveys_df.iloc[0:3, [1,3,8]]
surveys_df.loc[5, 'species_id']
surveys_df.iloc[5, 5]
surveys_df.iloc[0:3, 0:9:3]
surveys_df.loc[0:3, ['sex','day','species_id']]
surveys_df.iloc[1:4, :2]
surveys_df.loc[1:4, ['record_id','month']]
surveys_df.loc[[1,3,5],['species_id','sex']]
surveys_df.iloc[[1,3,5],[5,6]]
yr2002 = surveys_df[surveys_df.year == 2002]
yr2002
ol_obs = surveys_df[surveys_df['species_id'] == 'OL']
surveys_df[(surveys_df.sex == 'M') & (surveys_df.year <= 1985)]
surveys_df[surveys_df.year != 2002]
surveys_df[(surveys_df['weight'] <= 8) & (surveys_df['year'] == 1999)]
surveys_df['new_col'] = 'NaN'
for index, row in surveys_df.iterrows():
    surveys_df.loc[index,'new_col'] = "v"
surveys_df.head(6)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18562874251497005, 'fraction_numerical': 0.030605455755156354, 'mean_word_length': 9.477351916376307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '70238', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1316, 'n_words': 172, 'path': 'python-bcb546/pandas-lesson-Week09-TAH.ipynb', 'repo_name': 'EEOB-BioData/BCB546X-Fall2017', 'size': '3006'}