{"id":3409,"date":"2021-02-19T21:47:36","date_gmt":"2021-02-19T20:47:36","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3409"},"modified":"2021-02-19T21:47:36","modified_gmt":"2021-02-19T20:47:36","slug":"accessing-elements-in-python-dataframe","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3409","title":{"rendered":"Accessing elements in Python dataframe"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">con = pyodbc.connect('DSN=AzureSQL;UID=tomvanmaanen;PWD=*******')\ncur = con.cursor()\nsql = 'SELECT * FROM AXYWARE.DEELNEMERS_CSV'\ndf = pd.read_sql(sql, con)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So, we have a table in a database (AXYWARE.DEELNEMERS_CSV) that is read in a dataframe in just 4 lines of code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">More or less the same holds for propagating the dataframe into a table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import create_engine<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">engine = create_engine(\"mssql+pyodbc:\/\/tomvanmaanen:*******!@AzureSQL\")\nff.to_sql('Tax_Exception__c', schema='RAPPORTAGE', con = engine, chunksize=100, method='multi', index=False, if_exists='replace')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-c302fe02-5c9b-4e36-95eb-3ef25f3f5efd\">Just 3 lines of code, including importing a library to propagate a dataframe (ff) to a table (Tax_Exception__c).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To access an element within a dataframe, we may either use the labels or the position. So we may use a label approach:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.loc[:,\"FirstName\"] to derive a column or df.loc[3,\"FirstName\"] to access a element<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or we may use the positions:<\/p>\n\n\n\n<pre id=\"block-aaf531fe-5c5b-40c2-9a75-a5094246fa43\" class=\"wp-block-preformatted\">df.iloc[:,3] to derive a column or df.loc[3,4] to access a element<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In most cases, the label to identify a row is the same number that is used to identify a position. So:<\/p>\n\n\n\n<pre id=\"block-a09bbb90-f2a8-43f5-8a2d-37d26fe280e2\" class=\"wp-block-preformatted\">df.iloc[3] equals df.loc[3]\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is also possible to use this type of coding to update a certain slice of your data frame:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.loc[(df['BRPStreetName__c'] == 'Waterlelie') &amp; (df['BRPCityName__c'] == 'GOUDA'),'LastName'] = 'testName'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Somehow, the &#8216;loc&#8217; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(&#8216;DSN=AzureSQL;UID=tomvanmaanen;PWD=*******&#8217;) cur = con.cursor() sql = &#8216;SELECT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3409","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3409"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3409\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3409"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}