Accessing elements in Python dataframe

A dataframe in Python can be compared to a table in a database. I also get idea that database functionalities are translated into functions that can be found in dataframes. It is also true that a database table can be quickly read into a dataframe. See:

con = pyodbc.connect('DSN=AzureSQL;UID=tomvanmaanen;PWD=*******')
cur = con.cursor()
sql = 'SELECT * FROM AXYWARE.DEELNEMERS_CSV'
df = pd.read_sql(sql, con)

So, we have a table in a database (AXYWARE.DEELNEMERS_CSV) that is read in a dataframe in just 4 lines of code.

More or less the same holds for propagating the dataframe into a table:

from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://tomvanmaanen:*******!@AzureSQL")
ff.to_sql('Tax_Exception__c', schema='RAPPORTAGE', con = engine, chunksize=100, method='multi', index=False, if_exists='replace')

Just 3 lines of code, including importing a library to propagate a dataframe (ff) to a table (Tax_Exception__c).

To access an element within a dataframe, we may either use the labels or the position. So we may use a label approach:

df.loc[:,"FirstName"] to derive a column or df.loc[3,"FirstName"] to access a element

Or we may use the positions:

df.iloc[:,3] to derive a column or df.loc[3,4] to access a element

In most cases, the label to identify a row is the same number that is used to identify a position. So:

df.iloc[3] equals df.loc[3]

However, the philosophy is different. With iloc we address a position. With loc, we address a label. As the label equals the position, the code is equal.

It is also possible to use this type of coding to update a certain slice of your data frame:

df.loc[(df['BRPStreetName__c'] == 'Waterlelie') & (df['BRPCityName__c'] == 'GOUDA'),'LastName'] = 'testName'

Somehow, the ‘loc’ method seems to refer to the same memory position as the original dataframe. this then means that if a slice of the dataframe is referred to by .loc, we may change the content.

Door tom