



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




[2]: import pandas as pd
[10]: #instantiating a pandas dataframe object df = pd.DataFrame() #creating a column of numeric attributes df['num1'] = [10, 23, 45, 90, 46, 34, 10] #creating a column with fractional variables df['frac'] = [0.234, 0.123, 0.4353, 21.34, 45.00, 30.20, 90.045] #creating categorical columns df['cat1'] = ['a', 'b', 'c', 'd', 'a', 'c', 'b'] df['cat2'] = ['x', 'y', 'x', 'x', 'y', 'y', 'x'] #creating a dummy datetime column df['date_col'] = ['1990-08-3', '2000-07-21', '1998-06-17', '2021-06-30',␣ ↪→'1776-07-4', '2001-09-11', '2010-02-17']
Seeing the results of your dataframe * You can then print out basic aspects of your dataframe using different pandas functions. * These include .columns to print the column names, .head(n) to show the first n columns, .tail(n) for otherwsie and many more. * Few are explored here
[11]: #displaying the first 3 columns df.head(3)
[11]: num1 frac cat1 cat2 date_col 0 10 0.2340 a x 1990-08- 1 23 0.1230 b y 2000-07- 2 45 0.4353 c x 1998-06-
[12]: #A statistical summary of your numeric columns can be obtained as: df.describe()
[12]: num1 frac count 7.000000 7. mean 36.857143 26. std 27.739434 32. min 10.000000 0. 25% 16.500000 0. 50% 34.000000 21. 75% 45.500000 37. max 90.000000 90.
[14]: #to determine the memory consumption of each colum in bytes:- this is important ␣ ↪→ in evaluating the suitability of your computing resources, df.memory_usage(deep= True )
[14]: Index 128 num1 56 frac 56 cat1 406 cat2 406 date_col 467 dtype: int
[15]: #To know the datatypes of our columns: df.info()
RangeIndex: 7 entries, 0 to 6 Data columns (total 5 columns):
0 num1 7 non-null int 1 frac 7 non-null float 2 cat1 7 non-null object 3 cat2 7 non-null object 4 date_col 7 non-null object dtypes: float64(1), int64(1), object(3) memory usage: 408.0+ bytes Pro tip: Switching from one dataframe can be achieved through .astype functionality
df_copy_original.head(2)
[28]: num1 frac cat1 cat2 date_col num 0 10 0.234 a x 1990-08-3 10. 1 23 0.123 b y 2000-07-21 23.
Pro tip: To concatenate dataframes vertically, change the axis argument to 1 Pandas supports a number of descriptive analytics functionalities. Sampling them out:
[32]: #To find the unique values and their count in pandas: print('The unique values in the num1 columns are: ' +str(df['num1'].unique())) print("Their count is: " + str(df['num1'].nunique()))
The unique values in the num1 columns are: [10 23 45 90 46 34] Their count is: 6
[39]: #We can visualize the distribution of the unique values in categorical columns: df['cat2'].value_counts().plot(kind='barh')
[39]:
[41]: #duplicate values can be dropped using duplicates = df.drop_duplicates() duplicates
[41]: num1 frac cat1 cat2 date_col num 0 10 0.2340 a x 1990-08-3 10. 1 23 0.1230 b y 2000-07-21 23. 2 45 0.4353 c x 1998-06-17 45. 3 90 21.3400 d x 2021-06-30 90. 4 46 45.0000 a y 1776-07-4 46. 5 34 30.2000 c y 2001-09-11 34. 6 10 90.0450 b x 2010-02-17 10.
2 Handling Date and time columns
Given data containing date time columns, one should first ensure that the columns are in the correct format by running them through the .date_time function:
[44]: df['date_col'] = pd.to_datetime(df['date_col']) #now checking the new data type df.info()
RangeIndex: 7 entries, 0 to 6 Data columns (total 6 columns):
0 num1 7 non-null int 1 frac 7 non-null float 2 cat1 7 non-null object 3 cat2 7 non-null object 4 date_col 7 non-null datetime64[ns] 5 num3 7 non-null float dtypes: datetime64ns, float64(2), int64(1), object(2) memory usage: 464.0+ bytes
[45]: #we can create a month, day and year column from this data as: df['month'] = df['date_col'].dt.month_name() df['year'] = df['date_col'].dt.year df['day'] = df['date_col'].dt.day df.head(2)
[45]: num1 frac cat1 cat2 date_col num3 month year day 0 10 0.234 a x 1990-08-03 10.0 August 1990 3 1 23 0.123 b y 2000-07-21 23.0 July 2000 21
Group-by function * We could obtained various information on their respective classes using the groupby function. * This function takes the column or columns to be grouped by as well as the statistical information to be extracted from the selected group. * Example: