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.