Skip to content

Instantly share code, notes, and snippets.

View datagy's full-sized avatar

datagy

View GitHub Profile
@datagy
datagy / total.py
Created October 21, 2022 11:40
Adding a total row
import pandas as pd
df = pd.DataFrame({
'Name': ['Nik', 'Kate', 'Evan'],
'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'],
'Total': [99.99, 125.65, 33.43]
})
df['Over 100'] = ['Yes' if Total > 100 else 'No' for Total in df['Total']]
df[['City', 'State']] = df['Location'].str.split(', ', expand=True)
@datagy
datagy / texttocolumns.py
Created October 21, 2022 11:28
Replicating Text to Columns
# Using text-to-columns in Pandas
import pandas as pd
df = pd.DataFrame({
'Name': ['Nik', 'Kate', 'Evan'],
'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'],
'Total': [99.99, 125.65, 33.43]
})
df['Over 100'] = ['Yes' if Total > 100 else 'No' for Total in df['Total']]
@datagy
datagy / if in pandas.py
Created October 21, 2022 11:19
Using an IF Statement in Pandas
# Using if Statements in Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Nik', 'Kate', 'Evan'],
'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'],
'Total': [99.99, 125.65, 33.43]
})
df['Over 100'] = ['Yes' if Total > 100 else 'No' for Total in df['Total']]
@datagy
datagy / sample.py
Last active October 21, 2022 11:11
Loading a Sample Pandas DataFrame
# Loading a Sample Pandas DataFrame
import pandas as pd
df = pd.DataFrame({
'Name': ['Nik', 'Kate', 'Evan'],
'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'],
'Total': [99.99, 125.65, 33.43]
})
print(df)
{
"If Name == Main": {
"prefix": "ifn",
"body": [
"if __name__ == '__main__':",
" $0"
],
"description": "If Name == Main"
}
}
{
"Filter a Pandas DataFrame": {
"prefix": "pf",
"body": [
"${1:df} = ${1:df}[${1:df}['${2:Column}'] ${3|==, >, >=, <, <=, !=|} ${4:Filter}] "
],
"description": "Filter a Pandas DataFrame"
}
}
{
"Modify a Pandas Column": {
"prefix": "pdc",
"body": [
"${1:df_name}['${2:col_name}'] = ${1:df_name}['${2:col_name}'].$0"
],
"description": "Modifies a Pandas Column"
}
}
{
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
@datagy
datagy / pandas_reading04.py
Created September 22, 2022 22:59
Reading columns as datetime
# Reading Multiple Columns As Date Time
import pandas as pd
df = pd.read_excel(
'Pandas_read_files_tips.xlsx',
parse_dates={'Date': ['Month', 'Day', 'Year']}
)
print(df)
# Returns:
@datagy
datagy / pandas_reading03.py
Last active September 22, 2022 22:47
Read Multiple Excel Files at Once
import pandas as pd
import os
# Create list of files
file_path = '/Desktop/Files/'
files = [os.path.join(file_path, file) for file in os.listdir(file_path)]
# Join DataFrames Using pd.concat()
df = pd.concat([pd.read_excel(file) for file in files], ignore_index=True)
print(df.head())