"""
# Introduction

Content:

1. [Show installed versions](#1)
2. [Create an example DataFrame](#2)
3. [Rename columns](#3)
4. [Reverse row order](#4)
5. [Reverse column order](#5)
6. [Select columns by data type](#6)
7. [Convert strings to numbers](#7)
8. [Reduce DataFrame size](#8)
"""
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv
from glob import glob

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
drinks = pd.read_csv('http://bit.ly/drinksbycountry')
movies = pd.read_csv('http://bit.ly/imdbratings')
orders = pd.read_csv('http://bit.ly/chiporders', sep='\t')
orders['item_price'] = orders.item_price.str.replace('$', '').astype('float')
stocks = pd.read_csv('http://bit.ly/smallstocks', parse_dates=['Date'])
titanic = pd.read_csv('http://bit.ly/kaggletrain')
ufo = pd.read_csv('http://bit.ly/uforeports', parse_dates=['Time'])
"""
<a id="1"></a><br>
## 1. Show installed versions
"""
pd.__version__
pd.show_versions()
"""
<a id="2"></a><br>
## 2. Create an example DataFrame
"""
df=pd.DataFrame({"col one":[100,200], "col two":[300,400]})
df
pd.DataFrame(np.random.rand(4,8))
pd.DataFrame(np.random.rand(4,5),columns=list("queen"))
"""
<a id="3"></a><br>
## 3. Rename Columns
"""
df
df =df.rename({"col one":"col_one","col two":"col_two"},axis="columns")
df
df.columns=["colone","coltwo"]
df
df.columns = df.columns.str.replace("col","wol")
df
df.add_prefix("x_")
df.add_suffix("x_")
"""
<a id="4"></a><br>
## 4. Reverse Row Order
"""
drinks.head()
drinks.loc[::-1].head()
drinks.loc[::-1].reset_index(drop=True).head()
"""
<a id="5"></a><br>
## 5. Reverse Column Order
"""
drinks.head()
drinks.loc[:,::-1].head()
"""
<a id="6"></a><br>
## 6. Select columns by data type
"""
drinks.dtypes
drinks.select_dtypes(include="number").head()
drinks.select_dtypes(include="object").head()
drinks.select_dtypes(include=["number","object","category","datetime"]).head()
drinks.select_dtypes(exclude="number").head()
"""
<a id="7"></a><br>
## 7. Convert Strings to Numbers
"""
df=pd.DataFrame({"col_one":["1.1","2.2","3.3"],
                 "col_two":["4.4","5.5","6.6"],
                 "col_three":["7.7","8.8","-"]
                })
df
df.dtypes

df.astype({"col_one":"float","col_two":"float"}).dtypes
pd.to_numeric(df.col_three,errors="coerce")
pd.to_numeric(df.col_three,errors="coerce").fillna(0)
df=df.apply(pd.to_numeric,errors="coerce").fillna(0)
df
df.dtypes
"""
<a id="8"></a><br>
## 8. Reduce DataFrame size
"""
drinks.info(memory_usage="deep")
cols=["beer_servings","continent"]
small_drinks=pd.read_csv("http://bit.ly/drinksbycountry",usecols=cols)
small_drinks.info(memory_usage="deep")
dtypes={"continent":"category"}
smaller_drinks=pd.read_csv("http://bit.ly/drinksbycountry",usecols=cols,dtype=dtypes)
smaller_drinks.info(memory_usage="deep")