<jupyter_start><jupyter_text>IndexingOn [Kaggle](https://www.kaggle.com/residentmario/indexing-selecting-assigning)Both **loc** and **iloc** are row-first, column-second. This is the opposite of what we do in native Python, which is column-first, row-second. **loc is label-based** while **iloc is integer index based**.<jupyter_code>import numpy as np
import pandas as pd

arr = np.random.randn(4, 3) * 10
df = pd.DataFrame(arr)
df2 = pd.DataFrame(arr,columns=list('ABC'))
print(arr)
print(df)
print(df.loc[2,1], df.iloc[2,1], df2.loc[2,'B'], df2.iloc[2,1], arr[2,1])
print('\narr[2,]', type(arr[2,]))
print(arr[2,])

print('\ndf.loc[2,1]',type(df.loc[2,1]),df.loc[2,1]) # numpy.float64, 2 integer index returns the cell 
print('\ndf.loc[2,]',type(df.loc[2,])) # Series, integer index n in first plce returns n-th row
print(df.loc[2,])

print('\ndf.loc[:,1]',type(df.loc[:,1])) # Series, ':' in first place and integer index n in second plce returns n-th column
print(df.loc[:,1])

print('\ndf.loc[:,:1]',type(df.loc[:,:1])) # DataFrame, both first and second are ranges or list
print(df.loc[:,:1])

print("\ndf2.loc[:,['A','B']]",type(df2.loc[:,['A','B']])) # DataFrame, both first and second are ranges or list
print(df2.loc[:,['A','B']])<jupyter_output>arr[2,] <class 'numpy.ndarray'>
[13.65014957 -6.43849941 -1.52890047]

df.loc[2,1] <class 'numpy.float64'> -6.438499408073266

df.loc[2,] <class 'pandas.core.series.Series'>
0    13.650150
1    -6.438499
2    -1.528900
Name: 2, dtype: float64

df.loc[:,1] <class 'pandas.core.series.Series'>
0    8.693577
1   -0.327727
2   -6.438499
3    7.577255
Name: 1, dtype: float64

df.loc[:,:1] <class 'pandas.core.frame.DataFrame'>
           0         1
0  -5.400252  8.693577
1   5.427399 -0.327727
2  13.650150 -6.438499
3  -1.113487  7.577255

df2.loc[:,['A','B']] <class 'pandas.core.frame.DataFrame'>
           A         B
0  -5.400252  8.693577
1   5.427399 -0.327727
2  13.650150 -6.438499
3  -1.113487  7.577255<jupyter_text>**Data conversion**: float + numpy.float64 -> numpy.float64<jupyter_code>t = 3 + df.iloc[2,1]
s = 3 + float(df.iloc[2,1])
print(type(t),t, type(s),s)<jupyter_output><class 'numpy.float64'> -3.438499408073266 <class 'float'> -3.438499408073266<jupyter_text>read_csvStart with **head()**, to decide whetehr index is in the csv, add **index_col** parameter as needed. Then use **shape, dtypes and describe()** to have a better picture of the dataframe.<jupyter_code>reviews = pd.read_csv("data/winemag-data-130k-v2.csv", index_col=0)
reviews.head(3)
print(reviews.shape)
print(reviews.dtypes)
reviews.describe()
reviews.loc[:5, ['taster_name', 'taster_twitter_handle', 'points']]<jupyter_output><empty_output><jupyter_text>Stock Prices<jupyter_code>ibm = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=IBM&apikey=demo&datatype=csv',index_col=0)
ibm<jupyter_output><empty_output><jupyter_text>Iteration throught DataframeTry NOT to use, use **Windows functions** instead<jupyter_code>for index, row in ibm[:10].iterrows():
    print(row["open"], row["volume"])<jupyter_output>120.7 38063533.0
130.12 12819233.0
129.7 5598705.0
129.28 5397956.0
128.28 4905506.0
128.02 7503180.0
129.15 7677739.0
129.09 3729068.0
127.95 5602466.0
128.57 4676487.0<jupyter_text>Window functions- shift(n)- expanding()- rolling(n)https://towardsdatascience.com/window-functions-in-pandas-eaece0421f7<jupyter_code>stock_df = pd.DataFrame([100, 98, 95, 96, 99, 102, 103, 105, 105, 108], columns=['price'])
print(stock_df)
stock_df['prev_price'] = stock_df.shift(1)
print(stock_df)
stock_df['daily_return'] = stock_df['price']/stock_df['prev_price']-1
stock_df['expand_mean']=stock_df['daily_return'].expanding().mean()
stock_df['roll_mean_3']=stock_df['daily_return'].rolling(3).mean()
stock_df
stock_df['balance']=0
stock_df.loc[0,'balance']=10000
stock_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18415637860082304, 'fraction_numerical': 0.1301440329218107, 'mean_word_length': 5.580372250423012, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 31, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '570434', 'license': 'Apache-2.0', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1629, 'n_words': 305, 'path': 'bigdata/2A_Pandas.ipynb', 'repo_name': 'cliffweng/coding-dojo', 'size': '3888'}