-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandas_ex.py
200 lines (140 loc) · 3.92 KB
/
pandas_ex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
"""Pandas.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1T4Lqw6FGoeU8s6RQzG7IVWGlrFI1lCC8
"""
import pandas as pd
df = pd.read_csv('files/sales.csv')
#second param
# skiprows = 5 not read the fifth first rows
# nrows = 10 just read first 10 rows
# skip_blank_rows = True
# error_bad_lines = True
# warn_bad_lines = True
#first 5 rows
a = df.head()
print(a)
#first n rows
a = df.head(2)
print(a)
# last 5 rows
a = df .tail()
print(a)
# last n rows
a = df .tail(1)
print(a)
# numpy -> ndarray
# pandas -> series(one column), dataframe (more than one column)
product = df["Product"]
print(product)
product = df.Product
print(product)
a = type(df)
print(a)
a = df.describe()
print(a)
a = df.describe().T
print(a)
a = df.info
print(a)
a = df.columns
print(a)
a = df.Product.value_counts()
print(a)
a = df.nunique()
print(a)
a = df.Product.unique()
print(a)
a = len(df.Product.unique())
print(a)
a = list(df.columns)
print(a)
a = df.Company.unique()
print(a)
a = df[["Product","Company"]]
print(a)
# return True or False
print(df.Company == "Pepsi")
# return dataframe
print(df[df.Company == "Pepsi"])
print(df.loc[df.Product == "Cola"])
print((df.loc[df.Product == "Cola"]["Company"]).unique())
print(df.loc[df.Product == "Cola"][["Company","Category"]])
print(df["Units Sold"].max())
print(df[df["Units Sold"] == df["Units Sold"].max()][["Product","Units Sold"]])
a = df.sample(5)
print(a)
# 50% from df
a = df.sample(frac = 0.5)
print(a)
# Include all the databases, but in a different order than before.
a = df.sample(frac = 0.5)
print(a)
a = df.Product.value_counts()[:4]
print(a)
a = df[df["Units Sold"] >= 100]["Product"]
print(a)
a = df[df["Units Sold"] >= 100].count()
print(a)
a = df[df["Units Sold"] >= 1].Revenue.mean()
print(a)
a = df[df["Revenue"] == df["Revenue"].max()]["Units Sold"].value_counts().max()
print(a)
a = df.groupby('Product').sum()["Units Sold"]
print(a)
a = df.groupby('Category').max()["Revenue"]
print(a)
a = df.groupby(['Product','Category']).sum()["Revenue"]
print(a)
a = df.groupby(['Product','Category'],as_index = False).sum()["Revenue"]
print(a)
a = df.groupby(['Product','Category']).agg(['sum', 'max','min'])["Revenue"]
print(a)
# If a column with this name exists, its values are updated; otherwise, a new column is created.
df['Profit-status'] = pd.cut(df.Profit.sort_values(), labels = ['bad' , 'normal', 'good','very good'],bins = 4)
print()
bad = df[df["Profit-status"] == "bad"][["Profit","Profit-status"]]
print(bad)
print()
normal = df[df["Profit-status"] == "normal"][["Profit","Profit-status"]]
print(normal)
print()
good = df[df["Profit-status"] == "good"][["Profit","Profit-status"]]
print(good)
print()
veryGood = df[df["Profit-status"] == "very good"][["Profit","Profit-status"]]
print(veryGood)
df['Profit-status'] = pd.cut(df.Profit.sort_values(),
labels = ['bad' , 'good', 'normal','very good'],
bins = [-float("inf"), 0 , 1000, 2000,float("inf") ])
print()
bad = df[df["Profit-status"] == "bad"][["Profit","Profit-status"]]
print(bad)
print()
good = df[df["Profit-status"] == "good"][["Profit","Profit-status"]]
print(good)
print()
normal = df[df["Profit-status"] == "normal"][["Profit","Profit-status"]]
print(normal)
print()
veryGood = df[df["Profit-status"] == "very good"][["Profit","Profit-status"]]
print(veryGood)
def checkProfit(profit):
if profit <= 0:
ret = "Bad"
elif profit > 0 and profit <= 1000:
ret = "Good"
elif profit > 1000 and profit <= 3000:
ret = "Normal"
else:
ret = "Very Good"
return ret
df["Profit_Status"] = df.Profit.apply(checkProfit)
a = df[["Profit","Profit_Status"]].sort_values(by = "Profit").reset_index()
print(a)
print()
a = df.groupby(['Product','Category']).agg(['max'])["Revenue"].plot(kind="bar")
print(a)
a = df.groupby(['Product','Category']).agg(['max'])["Revenue"].hist()
print(a)