<jupyter_start><jupyter_text>Indexing and types notebook<jupyter_code># Connect my Google Drive to Google Colab
from google.colab import drive
drive.mount ('/content/gdrive')
# Change the working directory
%cd /content/gdrive/MyDrive/swd1a-python-2021-10
# Check the working directory
!pwd<jupyter_output>/content/gdrive/.shortcut-targets-by-id/1c-UzOKbLCZjy_P7oCGtvGKn-RoOvpDyC/swd1a-python-2021-10<jupyter_text>Challenges review<jupyter_code># fibonacci sequence generating function
a= list(range(10))

for i in range(0, len(a)-2, +1):
  a[i+2] = a[i+1] + a[i]
print(a)
list(range(10))
def fibbo_seq(a):

  fibbo_seq = [0, 1]

  if a == 0:

    print("Error")

  elif a == 1:

    print(fibbo_seq[a-1])

  elif a == 2:

    print(fibbo_seq)

  else:

    for i in range (2,a):

      next = fibbo_seq[i-1] + fibbo_seq[i-2]

      fibbo_seq.append(next)

    print(fibbo_seq)
fibbo_seq(10)
n = int(input("Enter your given length of a fibonacci sequence: "))

s1 = 0

s2 = 1

sum = 0

count = 1

while(count <= n):

  print(sum, end = " ")

  count += 1

  s1 = s2

  s2 = sum

  sum = s1 + s2
def fib_rec(n):

  if n < 2:
    return n

  else:
    return fib_rec(n - 1) + fib_rec(n - 2)
fib_rec(10)
for x in range(10):
  print(fib_rec(x))
[fib_rec(x) for x in range(10)]
def fibs(n=10):
  a,b = 0, 1
  for x in range(n):
    print(a)
    a, b = b, a + b

fibs(10)<jupyter_output>0
1
1
2
3
5
8
13
21
34<jupyter_text>Challenge 2: summary data1. How many recorded individuals are female `F` and how many male `M`?2. What happens when you group by two columns using the following syntax and then calculate mean values?* grouped_data2 = surveys_df.groupby(['plot_id', 'sex'])* grouped_data2.mean()3. Summarise weight values for each site in your data. HINT: you can use the following syntax to only create summary statistics for one column in your data. by_site['weight'].describe()<jupyter_code># import the pandas library
import pandas as pd

survey_data = pd.read_csv("data/surveys.csv")
grouped_data = survey_data.groupby('sex')
grouped_data['sex'].count()
grouped_data['sex'].describe()[['count']]
datagroup = grouped_data.count()
Males = datagroup.iloc[1,1]
Males
grouped_data2 = survey_data.groupby(['plot_id', 'sex'])
grouped_data2.mean()
by_site = survey_data.groupby(['plot_id'])
by_site['weight'].describe()
# .count() method returns the counts of a given row

species_counts = survey_data.groupby('species_id')['record_id'].count()
species_counts
survey_data['species_id'].value_counts()

# doing maths with pandas dataframes

survey_data.dtypes
survey_data['weight']
survey_data['weight'] * 2
(survey_data['weight'] - survey_data['weight'].mean() )/ survey_data['weight'].std()
# visualising data

species_counts = survey_data.groupby('species_id')['record_id'].count()

species_counts.plot(kind="bar")
total_count = survey_data.groupby('plot_id')['record_id'].nunique()

total_count.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>Indexing and slicing<jupyter_code># example of indexing
numbers = [1,2,3]
numbers[0]
survey_data['hindfoot_length']
survey_data.hindfoot_length
surveys_species = survey_data['species_id']
survey_data.hindfoot_length.weight
survey_data[['hindfoot_length', 'species_id']]
survey_data['hindfoot_lengths']
survey_data.columns
col_label = 'hindfoot_lengths'

# to check if the label is in the columns

col_label in survey_data.columns
# select rows with slicing
# data[start:stop]

survey_data[0:3]
survey_data['species_id'][1]
survey_data[0,0:5]
survey_data['species_id'][1:10]
# making a copy
survey_data_copy = survey_data.copy()
# making a reference
ref_data_copy = survey_data
survey_data.head()
ref_data_copy[0:3] = 0
ref_data_copy.head()
survey_data.head()
survey_data = survey_data_copy.copy()
# the pandas way to do slicing and subsetting
#.iloc indexing with integers

survey_data.iloc[0:3,1:4]
# label based indexing
# .loc[rows, columns]

survey_data.loc[[0, 10], ['hindfoot_length','weight']]
survey_data.loc[5:15, :]
survey_data.iloc[5, 4]
survey_data.loc[5,'weight']
surveys_df = survey_data.copy()
surveys_df[0:1]
surveys_df[:4]
surveys_df[:-1]
surveys_df.iloc[0:4, 1:4]
surveys_df.loc[0:4, 1:4]
surveys_df.head()
surveys_df[surveys_df.year == 2002]
surveys_df[surveys_df.year != 2002]
surveys_df[(surveys_df.year >= 1980) & (surveys_df.year <= 1985)]
surveys_df.year >= 1980
pd.isnull(surveys_df)
surveys_df.isna()
surveys_df[pd.isnull(surveys_df)]
mask = pd.isnull(surveys_df).any(axis=1)
surveys_df[~mask]
surveys_df.dropna()<jupyter_output><empty_output><jupyter_text>Data types and formats<jupyter_code>surveys_df.dtypes
type(surveys_df)
surveys_df['sex'].dtype
surveys_df['weight'].dtype
# took an integer column and added to a float column

surveys_df['year'] + surveys_df['hindfoot_length']
2 + 2.0
surveys_df['year'] + surveys_df['sex']
surveys_df['record_id'].dtype
surveys_df['record_id'] = surveys_df['record_id'].astype('float64')
surveys_df['record_id'].dtype
surveys_df['hindfoot_length'].isna().sum()
surveys_df.isna().sum()
surveys_df['hindfoot_length'].dtype
surveys_df['hindfoot_length'].astype('int64')
surveys_df['hindfoot_length'].mean()
# droping Nans
surveysNoNaN = surveys_df.dropna()
surveysNoNaN.isna().sum()
surveys_df_filled = surveys_df.fillna(0)
surveys_df_filled.isna().sum()
surveys_df_filled
surveysNoNaN.to_csv("data/cleaned_survey_data.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1426448736998514, 'fraction_numerical': 0.029160475482912333, 'mean_word_length': 6.18, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '164908', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2148, 'n_words': 428, 'path': 'notebooks/030_indexing_and_types.ipynb', 'repo_name': 'ARCTraining/python-2021-04', 'size': '5384'}