Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (2) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (31) javascript (3) Linux (167) Mac (19) Machine Learning (1) mySQL (48) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (47) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Tuesday, 13 November 2018

Python Pandas


Imports
import pandas as pd
from pandas import Series

#To run SQL queries in python
from pandasql import sqldf
pysqldf = lambda q: sqldf(q, globals())

query = '''
SELECT *
FROM myTable;
'''
result = pysqldf(query)



Read from File
pd.read_csv('path/to/file', headers=None)

Create DataFrame

Create Series

COUNT
df.sum(axis=1) # sum of row values
df.sum(axis=1) # sum of column values
df.shape[0] # Row count
df.shape[1] # Column count

Get Column by Index
df.iloc[: , [ 0 ] # first column
df.iloc[: , [ 0 , 4 ] ] #columns 0 and 4
df.iloc[row,column] # Returns cell value Example df.iloc[4,3], 4th row, 3rd column

Get Column by Label
df [ 'colLabel' ]
df.loc[:,"colLabel"]

Get Row by index
df.iloc[0] #first row

Get Row by Label
df.loc[ 'label' ]

Get Row where Column value Equals
df.loc[df['colLabel'] == value]
df[df['colLabel'] == value] 

Get Row where Column value IS IN values
df.loc[df['coLabel'].isin(values)]
df.loc[(df['colLabel'] == value) & df['other_colLabel'].isin(values)] 

DataFile to SQL
mydf_df.to_sql('tableName', DB_CONNECTION, if_exists='replace', index=False)
DB_CONNECTION is the database connection string

No comments:

Post a Comment

Note: only a member of this blog may post a comment.