Skip to content

Commit 5e26049

Browse files
Add files via upload
1 parent eb35118 commit 5e26049

9 files changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
3+
df = pd.DataFrame({'values_1': ['700','ABC','500','XYZ','1200'],
4+
'values_2': ['DDD','150','350','400','5000']
5+
})
6+
7+
df = df.apply (pd.to_numeric, errors='coerce')
8+
df = df.dropna()
9+
10+
print (df)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
df.reset_index(inplace=True)
2+
df = df.rename(columns = {'index':'new column name'})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pandas as pd
2+
3+
df["Week"] = pd.to_datetime(df['date']).dt.year.astype(str).str.cat(
4+
pd.to_datetime(df['date']).dt.week.astype(str).str.zfill(2), sep='-')

pandas/merge_two_csvs_with_pandas.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pandas as pd
2+
3+
4+
ips = pd.read_csv('Result1.csv', names=['IP','Domain','Country','Region','City','ISP','ASN'], encoding='ISO-8859-1')
5+
org_file = pd.read_csv('WA_2017_append.csv', names=['First Name', 'Last Name', 'DOB', 'IP'], encoding='ISO-8859-1')
6+
7+
merged_left = pd.merge(left=org_file,right=ips, how='left', left_on='IP', right_on='IP')
8+
merged_left.to_csv('WA_2017_append(result).csv', sep=',', encoding='utf-8', header=True)

pandas/read excel in pandas.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pandas as pd
2+
import os
3+
4+
df = pd.read_sql_table('temp_new', f"sqlite:///{os.path.join(working_dir, big_db_name)}")
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pandas as pd
2+
3+
df.to_sql(db_daily_table_name, small_db.con, if_exists='replace', index=False, chunksize=100)

pandas/shift.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
df = pd.DataFrame({"Col1": [10, 20, 15, 30, 45],
3+
"Col2": [13, 23, 18, 33, 48],
4+
"Col3": [17, 27, 22, 37, 52]},
5+
index=pd.date_range("2020-01-01", "2020-01-05"))
6+
7+
print(df)
8+
9+
df = df.shift(periods=3)
10+
print(df)

pandas/sort.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pandas as pd
2+
import numpy as np
3+
4+
df = pd.DataFrame({
5+
'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],
6+
'col2': [2, 1, 9, 8, 7, 4],
7+
'col3': [0, 1, 9, 4, 2, 3],
8+
'col4': ['a', 'B', 'c', 'D', 'e', 'F']
9+
})
10+
11+
df = df.sort_values(by=['col1'], inplace=True)
12+
13+
print(df)

pandas/split_apply.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import seaborn as sns
4+
import pandas as pd
5+
6+
sales_dict = {'colour': ['Yellow', 'Black', 'Blue', 'Red', 'Yellow', 'Black', 'Blue',
7+
'Red', 'Yellow', 'Black', 'Blue', 'Red', 'Yellow', 'Black', 'Blue', 'Red', 'Blue', 'Red'],
8+
'sales': [100000, 150000, 80000, 90000, 200000, 145000, 120000,
9+
300000, 250000, 200000, 160000, 90000, 90100, 150000, 142000, 130000, 400000, 350000],
10+
'transactions': [100, 150, 820, 920, 230, 120, 70, 250, 250, 110, 130, 860, 980, 300, 150, 170, 230, 280],
11+
'product': ['type A', 'type A', 'type A', 'type A', 'type A', 'type A', 'type A',
12+
'type A', 'type A', 'type B', 'type B', 'type B', 'type B', 'type B', 'type B', 'type B',
13+
'type B', 'type B']}
14+
15+
data_sales = pd.DataFrame(sales_dict)
16+
# print(data_sales)
17+
18+
ref_data = data_sales[data_sales.colour == 'Blue'][['sales', 'product']]
19+
ref_data = ref_data.rename(columns={"sales": "sales_1"})
20+
21+
def make_color(d, ref_data):
22+
# d['sales_2'] = d['sales'] - 1
23+
d = d.merge(ref_data, how='inner', on='product')
24+
return d
25+
26+
data_colour = data_sales.groupby('colour').apply(lambda x: make_color(x, ref_data))
27+
print(data_colour)

0 commit comments

Comments
 (0)