{"id":21528271,"url":"https://github.com/hrassi/pandas_starter","last_synced_at":"2025-03-17T18:44:50.252Z","repository":{"id":220785744,"uuid":"752596216","full_name":"hrassi/Pandas_starter","owner":"hrassi","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-04T10:21:42.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T06:09:44.442Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hrassi.png","metadata":{"files":{"readme":"readme.txt","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-04T10:08:52.000Z","updated_at":"2024-02-04T10:21:46.000Z","dependencies_parsed_at":"2024-02-04T10:54:27.165Z","dependency_job_id":null,"html_url":"https://github.com/hrassi/Pandas_starter","commit_stats":null,"previous_names":["hrassi/pandas_starter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2FPandas_starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2FPandas_starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2FPandas_starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2FPandas_starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hrassi","download_url":"https://codeload.github.com/hrassi/Pandas_starter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244091769,"owners_count":20396667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:52:26.162Z","updated_at":"2025-03-17T18:44:50.229Z","avatar_url":"https://github.com/hrassi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Python  Pandas and SQlite3 :\n\npandas tutorial : \nhttps://www.w3schools.com/python/pandas/default.asp\n\n\n\nadd-total-row-to-pandas-dataframe\n\nfilter-pandas-dataframe\n\n\nimport pandas as pd\nimport sqlite3\n\nconnection = sqlite3.connect(\"dental.db\")\n\n\n# to select all data from the table called patient :\n\ndf = pd.read_sql(\"select * from patient\",connection)\nprint(df)      # print(df.head(2)) or tail(2) for first 2 or last 2 row\n\n# to select the row containing “Houssam Rassi” from the collumn called “Name” :\n\nfiltered_row = df[ df[“Name\"] == \"Houssam Rassi\"]\nprint(filtered_row)\n\n# to select the row containing a part of “Houssam Rassi”\n# and case insensitive  from the collumn called “Name” :\n\nfiltered_row = df.query(\"Name.str.contains ('houss',case = False)\")\n\n# to select a row by its index number:\n\nfiltered_row = df.filter(items = [2], axis=0) # or item=[1,3] for 2 row 1 and 3\nor:\nprint(df.loc[2])  # use location 2 to print row 2\nor:\nprint(df.loc[1,2,3])  # use location 1,2,3 to print row 1,2,3\n\n\n# to remove the row nbr 3 from the data frame :\n\ndf = df.drop(3)\nor :\ndf.drop(x, inplace = True) # where x is the index\n                                                        # inplace true correct\n                                                        # directly the same file\n\n# to set in the 3rd row in the ‘paid’ column the value 100  \n    use df.loc :\n\ndf.loc [3, ’paid’ ] = 100\n\n\n# from pandas to sqlite db file : \n \nfiltered_row.to_sql(\"Patient\",con=connection,if_exists='replace')\nconnection.commit()  # save\nconnection.close()\n\n# to open old excel file from old access db (sepcify engine ‘xlrd’) :\npip install xlrd\n\ndf = pd.read_excel(\"patient.xlsx\",engine='xlrd')\nprint(df)\n\n# then to save the file : \ndf.to_csv(\"patientxlrd.csv\")\n\n# to read the saved csv file :\ndf = pd.read_csv(\"patientxlrd.csv\")\n\n# SQLITE3 CREATING EMPTY db file and POPULATING IT:\n\n### CONNECTING TO DATABASE.db FILE OR CREATING (IF THE FILE DOES NOT EXIST WILL CREATE AN EMPTY DATABASE.db )\nconnection = sqlite3.connect(\"dentabase1.db\")\n### SAVING TABLE NAMED PATIENT TO THE FILE dentalbase1.db\ndf.to_sql(\"Patient\",con=connection,if_exists='replace')\nconnection.commit()  # save\n### LOADING ALL PATIENT TABLE FROM dentalbase1.db TO THE DATAFRAME df\ndf = pd.read_sql(\"select * from patient\",connection)\nprint (df)\nconnection.close()\n\n\nPANDAS Functions WIth Examples: \n**********************************************\n\nimport pandas as pd\n\n\n# to read excel file in dataframe :\ndata=pd.read_excel(\"in_out_eval.xlsx\")\ndata.to_csv(\"inouteval.csv\",sep=\"\\t\")\nprint(data)\n\n\n# to read xlsx excel file and save it to csv using xlrd engine (pip install xlrd):\ndf=pd.read_excel(\"patient.xlsx\",engine='xlrd')\n# to save dataframe df to .csv file :\ndf.to_csv(\"patientxlrd.csv\")\nprint(df)\n\n\n# load .csv file to dataframe df :\n# the optional index_col to remove \n# duplicate index collumn \ndf = pd.read_csv(\"patientxlrd.csv\", index_col=[0])\nprint (df)\n\nprint(df.head()) # or head(2) for first 2\nprint(df.tail()) # or tail(1) for last one\n\n# to print the type of dataframe\nprint(df.dtypes)\n\nprint(df['name'])\n\n# to print a specific row example for index 4\nprint(df.loc[4]) \n\n# set the index to be the name and overwrite the df : \ndf=df.set_index('name') \n# then print index Moubarak Eliana :\nprint(df.loc['Moubarak Eliana']) \n\n# to print all available column :\nprint(df.columns) :\n\n# to select only theses rows :\ndf=df[['Unnamed: 0', 'name', 'tel', 'referby', 'address']] \nprint(df)\n\n# to print idex row between 100 and 110\nprint (df.query('index \u003e 100 and index \u003c 110'))\n\n# to all row contaning case sensitif eliana :\nprint (df.query(\"name.str.contains ('eliana',case = False)\")) \n\n# to change data type of name column as interger:\ndf['name']=df['name'].astype('int') \n\n# put last row in a variable called:df_to_append \ndf_to_append=df.tail(1)\n# add a new row to df with append command containing the data of the variable df_to_append:\ndf=pd.concat([df,df_to_append])\nprint (df)\n\n#insert a new row in position 3 with value hello:\ndf.insert(2, \"Age\", \"hello\", True) \nprint (df)\n\n# to get the nmbr of the last available row to put data inside\nlast=(len(df.index))\nprint (last)\n# to remove collumns from dataframe and leave only first 3 collumn :\ndf=df[['Unnamed: 0', 'name', 'tel']]\nprint (df)\n# to add a row to the last available row in the dataframe name chakib....\ndf.loc[len(df.index)]=['5008','chakib','234234'] \nprint (df)\n\n#display max columns or rows, instead of none u can put the nbr of column\npd.set_option('display.max_columns',None) \npd.set_option('display.max_rows',None)\n\n# to read a single cell using index 1 and column\"name\"\nx=df.at[1,'name'] \nprint(x)\n\n# to write a single entry using index 1 and column\"name\"\ndf.at[1,'name']='tralla laa'\n# save to file( the optional index=False  is to remove the double index column):\ndf.to_csv(\"patientxlrd.csv\", index=False) \nprint (df)\n\n\n#  read cvs file and specifying datatype:\n(csv dont store data type when saving file)\ndf=pd.read_csv(´patientxlrd.csv’, \n                           dtype={‘name’ :´category ’ ,\n                                         ‘tel ‘ : ´ int64 ‘ , \n                                           ….. } )\n                                          \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrassi%2Fpandas_starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhrassi%2Fpandas_starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrassi%2Fpandas_starter/lists"}