In [1]:
import pandas as pd
import dateutil
import numpy as np
df = pd.read_csv('c:/1/phone_data.csv')
df.head(4)
Out[1]:
In [2]:
df['date'] = df['date'].apply(dateutil.parser.parse, dayfirst=True)
In [3]:
# How many rows and columns the dataset
df.shape
Out[3]:
In [4]:
# najdłuższa rozmowa telefoniczna
df['duration'].max()
Out[4]:
In [5]:
# chce się dowiedzieć co to była za rozmowa
df[df['duration']==df['duration'].max()]
Out[5]:
In [6]:
df['duration'][df['item'] == 'call'].sum()
Out[6]:
In [7]:
## wyświetlam 10 największych rozmów
df['duration'][df['item'] == 'call'].nlargest(10)
Out[7]:
In [8]:
# znajduje 10 najdłuższych rozmów
df.loc[(df['duration']>1200)&(df['item'] == 'call')]
Out[8]:
In [9]:
# znajduje 10 najdłuższych rozmów dla sieci Tesco
df['duration'][df['item'] == 'call'][df['network'] == 'Tesco'].nlargest(10)
Out[9]:
In [10]:
## Wszystkie rozmowy z sieci TEsco - 10 najwiekszych drugi sposób
df.loc[(df['item'] == 'call')&(df['network'] == 'Tesco')]['duration'].nlargest(10)
Out[10]:
In [11]:
# ile było rozmawiane w każdym miesiącu
df['month'].value_counts()
Out[11]:
In [12]:
# ile było rozmawiane w każdym miesiącu z każdej z sieci
df.pivot_table(index = ['month', 'network'], values='duration', columns='item', aggfunc=[np.sum])
Out[12]:
In [13]:
## ilu miałem operatorów?
df['network'].nunique()
Out[13]:
In [14]:
df.describe()
Out[14]:
In [15]:
df.pivot_table(index='network', values='duration', aggfunc=['sum', 'max','min', np.std, 'median'])
Out[15]:
In [16]:
# statystyka rozmów
df['duration'].agg(['min', 'max', np.mean, np.median, np.std])
Out[16]:
In [17]:
df.columns
Out[17]:
In [18]:
# użycie wykresy rozwarstwień
import seaborn as sns
sns.relplot(x='month', y='duration',hue='network', size='item', kind="line", data=df)
Out[18]:
Groupby
In [19]:
# jego sposób
df.groupby(['month']).groups.keys()
Out[19]:
In [20]:
# mój sposób
df['month'].unique()
Out[20]:
In [21]:
# jego sposób
len(df.groupby(['month']).groups['2014-11'])
Out[21]:
In [22]:
# mój sposób
df[df['month']=='2014-11']['month'].count()
Out[22]:
In [23]:
## pierwsze zapisy na rejestrze wg miesiąca
df.groupby('month').first()
Out[23]:
In [24]:
df.dtypes
Out[24]:
In [25]:
## dane są kompletne
df.isnull().sum()
Out[25]:
In [26]:
## 10 najdłyższych rozmów
df.groupby('duration').first().tail(10)
Out[26]:
In [27]:
## ile trwały rozmowy w miesiącach
df.groupby('month')['duration'].sum()
Out[27]:
In [28]:
# ile było połączeń w miesiącach
df.groupby('month')['date'].count()
Out[28]:
In [29]:
# ile trwały tylko połączenia telefoniczne
df[df['item'] == 'call'].groupby('network')['duration'].sum()
Out[29]:
In [30]:
# ile było usług (m.in. połączeń) zależnie od typu w miesiącu
df.groupby(['month', 'item'])['date'].count()
Out[30]:
In [31]:
# # ile było usług (m.in. połączeń) zależnie od typu w miesiącu inny sposób
df.pivot_table(index=['month','item'], values='duration', aggfunc=['count'])
Out[31]:
In [32]:
## ile było usług według typu usług? TWORZENIE SERII
df.groupby(['month', 'network_type'])['date'].count()
Out[32]:
In [33]:
## ile było usług według typu usług? TWORZENIE TABELI DANYCH
df.groupby(['month', 'network_type'])[['date']].count()
Out[33]:
In [34]:
## ile było usług według operatorów?
df.pivot_table(index=['month','network_type'], values='duration', aggfunc=['count'])
Out[34]:
In [35]:
# tworenie serii
df.groupby('month')['duration'].sum()
Out[35]:
In [36]:
# tworzenie DataFrame
df.groupby('month')[['duration']].sum()
Out[36]:
In [37]:
# zadeklarowanie że nie chcemy indexu jako daty
df.groupby('month', as_index=False).agg({"duration": "sum"})
Out[37]:
In [38]:
## TEGO NIE UMIEM W TABELI PRZESTAWNEJ
## grupowanie zaawansowane
df.groupby(['month', 'item']).agg({'duration':sum, 'network_type': "count", 'date': 'first' })
Out[38]:
In [39]:
## grupowanie zaawansowane
df.groupby(['month', 'network']).agg({'duration':sum,'item':"count"})
Out[39]:
In [40]:
df.pivot_table(index=['month', 'network'], values='duration', aggfunc=['sum','count'])
Out[40]:
In [41]:
## grupowanie zaawansowane
df.groupby(['month', 'network','item']).agg({'duration':sum,'item':"count"})
Out[41]:
In [42]:
df.dtypes
Out[42]:
In [43]:
#df['date'] = df.date.astype(int)
In [44]:
aggregations = {'duration': lambda x: max(x) - 1}
df.groupby('month').agg(aggregations)
Out[44]:
In [45]:
# Najdłuższy czas połączeń wg miesięcy
aggregations = {'duration': lambda x: max(x)}
df.groupby('month').agg(aggregations)
Out[45]:
In [46]:
# całkowity czas połączeń wg miesięcy
aggregations = {'duration': lambda x: sum(x)}
df.groupby('month').agg(aggregations)
Out[46]:
In [47]:
# całkowity czas połączeń wg operatorów
aggregations = {'duration': lambda x: sum(x)}
df.groupby('network').agg(aggregations)
Out[47]:
In [48]:
# boskie
df.groupby(['month', 'item']).agg({'duration': [min, max, sum],'network_type': "count", 'date': [min, 'first', 'nunique']})
Out[48]:
In [49]:
# to jest zrozumiałe
grouped = df.groupby('month').agg({'duration': [min, max, np.mean]})
#grouped.columns = ["min_duration","max_duration", "mean_duration"]
#grouped.columns = grouped.columns.droplevel(level=0)
grouped.rename(columns={ "min": "min_duration", "max": "max_duration", "mean": "mean_duration"},inplace=True)
grouped.head()
Out[49]:
In [50]:
grouped = df.groupby('month').agg({'duration': [min, max, np.mean]})
# Using ravel, and a string join, we can create better names for the columns:
grouped.columns = ["_".join(x) for x in grouped.columns.ravel()]
grouped
Out[50]:
In [51]:
# Define the aggregation calculations
aggregations = {'duration': { 'total_duration': 'sum', 'average_duration': 'mean', 'num_calls': 'count'},
'date': { 'max_date': 'max', 'min_date': 'min', 'num_days': lambda x: max(x) - min(x)},
'network': ["count", "max"]}
df[df['item'] == 'call'].groupby('month').agg(aggregations)
Out[51]: