<jupyter_start><jupyter_text>Importing the library<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Reading in dataTo load data into python use pandas's ```read_..``` methods. There are plenty of read methods for different file formats.For example, loading data from excel..```pythonexcel_file = pd.ExcelFile('path_to_excel.xlsx')df = pd.read_excel(excel_file, 'Sheet1')```for other files try typing ```pd.read_``` and then hit the \ key **NOTE**: be careful when using the `\` (backslash) as path separator (default in Windows), as it is used to "escape" characters to give them new meaning, e.g. `\n` means new line.If you need to use backslash, put a 'r' in front of the string to have python read it "raw".<jupyter_code>pd.read_json(r'data\wine-reviews\winemag-data-130k-v2_tiny.json')<jupyter_output><empty_output><jupyter_text>Additional optionsAll the read methods have additional useful options.The main ones for `read_csv` (some of which are in common with other `read_..` methods): - `sep`: defines the type of column separator- `index_col`: defines which column should be read as the index (row label)- `usecols`: allows reading only a subset of the columns- `skiprows` and `skipfooter`: allows ignoring of initial/final rows- `nrows`: allows reading of a limited amount of rows- `na_values`: defines the values to be treated as null- `parse_dates`: defines the columns that contain dates to be parsed- `dayfirst`: defines that the day comes before the month in the date to be parsed- `thousands`: defines the thousands numeric separator- `decimal`: defines the decimal numeric separator- `encoding`: defines the type of encoding to be used, default utf-8.for more information consult the corresponding [documentation](https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html).**TIP**: if you get a `UnicodeDecodeError: 'utf-8' codec` error, try adding `encoding='latin'` to the read options, that usually solves the issue.<jupyter_code>pd.read_csv(
    'data/wine-reviews/winemag-data-130k-v2.csv',
    usecols=['wine_id', 'country', 'designation', 'price'],
    index_col='wine_id',
    nrows=5,
)<jupyter_output><empty_output><jupyter_text>***EXERCISE 2.1***Using the pandas documentation, find a way to read the file `data/wine-reviews/exercise_2.1.txt` in the following way:- read the file as tab-separated- parse the date in the `date of rating` column, in day/month format- set `country` as the index- skip the 3rd and 4th rows<jupyter_code># insert solution here<jupyter_output><empty_output><jupyter_text>Creating DataIn order to creating a pd.DataFrame from data we have to follow the contructor specifications.In general they can be created using:- dictionary- iterable (np.array, list, pd.Series..)<jupyter_code># using dictionary
data = {'Name': ['Bob', 'Mary'], 'Cats':[2,0], 'Dogs':[1,2]}

pd.DataFrame(data)
# using 2D iterable
data = [['Bob', 2, 1], ['Mary', 0, 2]]
labels = ['Name','Cats','Dogs'] 

pd.DataFrame(data, columns=labels)<jupyter_output><empty_output><jupyter_text>***EXERCISE 2.2***Create a DataFrame like the following:|    	| r   	| g   	| b  	| hex     	||--------	|-----	|-----	|----	|---------	|| blue   	| 0   	| 0   	| 1  	| 0000ff 	|| olive  	| 85  	| 107 	| 47 	| 556B2F 	|| sienna 	| 160 	| 82  	| 45 	| A0522D 	|<jupyter_code># insert solution here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13016344725111442, 'fraction_numerical': 0.01723625557206538, 'mean_word_length': 4.7342419080068145, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '612545', 'license': 'MIT', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1071, 'n_words': 376, 'path': '02_Read_Create_Data.ipynb', 'repo_name': 'gabrielecalvo/pandas_tutorial', 'size': '3365'}