0% found this document useful (0 votes)
7 views71 pages

Python Pandas Note

Pandas is a Python library designed for data manipulation and analysis, providing data structures like Series and DataFrame for handling structured data. The document outlines the installation process, basic functionalities, and attributes of Pandas Series, including how to create Series from various data types and check for properties like emptiness and presence of NaN values. Additionally, it covers essential functions such as mapping values, calculating standard deviation, converting Series to DataFrame, and counting unique values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7 views71 pages

Python Pandas Note

Pandas is a Python library designed for data manipulation and analysis, providing data structures like Series and DataFrame for handling structured data. The document outlines the installation process, basic functionalities, and attributes of Pandas Series, including how to create Series from various data types and check for properties like emptiness and presence of NaN values. Additionally, it covers essential functions such as mapping values, calculating standard deviation, converting Series to DataFrame, and counting unique values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Python Pandas What is Pandas? «& Pandas is a Python library used for working with data sets. It has functions for analyzing, cleaning, exploring, and manipulating data. @ Pandas is a Python library that simplifies data analysis and manipulation. It provides easy-to-use data structures and functions for working with structured data. With Pandas, you can organize, clean, and analyze data effectively, making it a valuable tool for data-related tasks Installation of Pandas Step 1: Check Python Version First, make sure you have Python installed on your system. Open a terminal or command prompt and run the following command to check the Python version: python --version Step 2: Open Command Prompt or Terminal Open the Command Prompt (Windows) or Terminal (Mac/Linux) on your computer. Step 3: Install Pandas Using pip To install Pandas, use the pip package manager. Run the following command: oxen camientntoatrine iA 184-4648 letters Aone ete m @ pip install pandas Step 4: Check Pandas Version To check the Pandas version, run the following command: @ import pandas as pd print(pd.__version_) Import Pandas(Pandas as pd) Once Pandas is installed, import it in your applications by adding the import keyword: --Pandas is usually imported under the pd alias. ‘@ import pandas as pd Python Pandas Data Structure Python Pandas provides two main data structures: Series and DataFrame. Series: @ ASeries is a one-dimensional labeled array that can hold any data type. Itis similar to a column ina spreadsheet or a single column of data in a NumPy array. The Series consists of two main components: the index and the data values. import pandas as pd oxen camientntoatrine iA 184-4648 letters Aone ete an # Creating a Series data = [10, 20, 30, 40, 50] s= [Link](data) print(s) Output 0 10 1 20 2 30 3 40 4 50 dtype: inte4 > DataFrame: @ ADataFrame is a two-dimensional labeled data structure with columns of potentially different data types. Itis similar to a table in a relational database or a spreadsheet with rows and columns. It can be thought of as a collection of Series objects where each Series represents a column. Series: Example import pandas as pd import numpy as np oxen camientntoatrine iA 184-4648 letters Aone ete wn # Create a Series from an array data_array = [Link]([10, 20, 30, 40, 50)) series_array = [Link](data_array) print("Series from array:") print(series_array) print() # Create a Series from alist data_list = [10, 20, 30, 40, 50] series_list = [Link](data_list) print("Series from list:") print(series_list) print() # Create a Series from a tuple data_tuple = (10, 20, 30, 40, 50) series_tuple=[Link](data_tuple) print("Series from tuple:") oxen camientntoatrine iA 184-4648 letters Aone ete print(series_tuple) print() # Create a Series from a dictionary data_dict={'A': 10, 'B': 20, 'C': 30, series_dict = [Link](data_dict) print("Series from dictionary:") print(series_dict) print() 50} #If we take the scalar values, then the index must be provided. # Create a Series with a scalar value scalar_value=5 series_scalar = [Link](scalar_value, index=['a\,'b 'c,'d),'e') print("Series with scalar value: print(series_scalar) Series from array: 0 10 oxen camientntoatrine iA 184-4648 letters Aone ete 1 20 2 30 3 40 4 50 dtype: intea Series from list: 0 10 1 20 2 30 3 40 4 50 dtype: intea Series from tuple: 0 10 1 20 2 30 3 40 oxen camientntoatrine iA 184-4648 letters Aone ete en 4 50 dtype: intea Series from dictionary: 10 20 30 40 50 dtype: intéa mooe> Series with scalar value: as bs 5 d5 e5 dtype: intea # Create a Series from a set oxen camiennoatrine tA 14-4648 lettuce HR AS-oNe eee To create a Series from a set, you need to provide an ordered collection, such as a list or an array, that maintains the desired order of the elements, Sets do not have an inherent order, so they cannot be directly converted into a Series. Ifyou have a set and want to create a Series, you need to convert the set into an ordered collection, such as.a list or an array, before creating the Series. import pandas as pd data_set={10, 20, 30, 40, 50} data_list=list(data_set) series_set = [Link](data_list) print("Series from set:") print(series_set) Series from set: 0 40 110 2 50 3 20 4 30 dtype: inte oxen camientntoatrine iA 184-4648 letters Aone ete wn Series object attributes & The Pandas Series object has several attributes that provide information about the series. Here's a description of some commonly used Series attributes: + @ [Link]:This attribute defines the index of the Series, which represents the labels for each element in the Series. + @ [Link]: Itreturs a tuple representing the shape of the Series, For a one-dimensional Series, the shape tuple will have only one element representing the length of the Series. + @ [Link]: It returns the data type of the elements in the Series. + @ [Link]: It returns the number of elements in the Series. + @ [Link]: It returns a boolean value indicating whether the Series is empty or not. Returns True ifthe Series is empty, and False otherwise. + @ [Link]: It returns a boolean value indicating whether there are any NaN (missing) values in the Series. Returns True if there are NaN values, and False otherwise, + @ [Link]: It returns the number of bytes consumed by the Series data + @ [Link]: It returns the number of dimensions in the Series data. For a Series, the ndim attribute will always be 1 oxen camientntoatrine iA 184-4648 letters Aone ete en It returns the size in bytes of each individual element in the Series. itemsi: Example import pandas as pd import numpy as np data=[10, 20, 30, 40, 50] series = [Link](data) print("Series:’ series) print("Index:, [Link]) print("Shapes", [Link]) print("Data Type: [Link]) print("Size: [Link]) print( "Is Empty, [Link]) print("Has NaN [Link]) print("Number of Bytes:”, [Link]) print("Number of Dimensions:”, [Link]) print("Item Size: [Link]) output Series: 0 10 oxen camennoatrine tA 4-468 lettuce HR ASo8e eee 1 20 2 30 3 40 4 50 dtype: inte Index: Rangelndex(start-0, stop-5, step=1) Shape: (5,) Is Empty: False Has NaNs: False Number of Bytes: 40 Number of Dimensions: 1 Item Size: 8 import pandas as pd # Create a Series data = 10, 20, 30, 40, 50) series = [Link](data) oxen camientntoatrine iA 184-4648 letters Aone ete wn # [Link] print("Index:" [Link]) #[Link] print("Shapes' [Link]) #[Link] print("Data Type: [Link]) #[Link] print("Size: [Link]) 4#[Link] print("Is Empty:”, [Link]) # [Link] print("Has NaNs:", [Link]) 4#[Link] print("Number of Bytes:”, [Link]) +#[Link] print("Number of Dimensions:", [Link]) oxen camientntoatrine iA 184-4648 letters Aone ete # [Link] print("Item Size: [Link]) angelndex(start=0, top=5, step=1) Shape: (5,) Data Type: inté4 Size:s Is Empty: False Has NaNs: False Number of Bytes: 40 Number of Dimensions: 1 Item Size: 8 In this example, we create a Series series from a list [10, 20, 30, 40, 50]. We then print the various attributes of the Series. + [Link] [J returns a Rangeindex object representing the index labels of the Series, in this case, a default range index from 0 to 4 + [Link] BJ returns a tuple (5,) indicating that the Series has 5 elements. + [Link] EJ returns int64 as the data type of the Series elements. + [Link] EJ returns 5 indicating the number of elements in the Series. oxen camientntoatrine iA 184-4648 letters Aone ete wan + [Link] G} returns False since the Series is not empty. [Link] [iJ returns False since there are no NaN (missing) values in the Series. «+ [Link] returns 40 indicating the number of bytes consumed by the Series data. + [Link] returns 1 indicating that the Series is one-dimensional. + [Link] [J returns 8 indicating the size in bytes of each individual element in the Series, which corresponds to the inté4 data type. Series object attributes Toretrieve specific information from a Series object in Pandas, you can use the following attributes and methods: Retrieving Index array and Data array: + Gi index array: You can access the index labels of a Series object using the [Link] attribute. It returns an Index object containing the index labels. + © Data array: Byou can access the data values ofa Series object using the [Link] attribute. It returns a NumPy array containing the data values. Example: import pandas as pd series = [Link](data) index_array = [Link] oxen camientntoatrine iA 184-4648 letters Aone ete wan data_array = [Link] print("Index Array:", Index array) print("Data Array:', data_array) Output: Index Array: Rangelndex(start=0, stop=5, step=1) Data Array: [10 20 30 40 50] Retrieving Types (dtype) and Size of Type (itemsize): + & Types (dtype): BiYou can retrieve the data type of the elements in a Series object using the [Link] attribute. It returns the dtype object representing the data type. Size of Type (itemsize): You can retrieve the size in bytes of each individual element in the Series using the [Link] attribute. Example: import pandas as pd data =[10, 20, 30, 40, 50) series = [Link](data) dtype =[Link] itemsize = series [Link] oxen camientntoatrine iA 184-4648 letters Aone ete wn print("Data Type’, dtype) print(*Item Size itemsize) Output: Data Type: inté4 Item Size: 8 Retrieving Shape, Dimension, Size, and Number of Bytes: + @ Shape: BB you can retrieve the shape of a Series object using the Series shape attribute. It returns a tuple representing the shape of the data. ensi Bou can retrieve the number of dimensions in the Series data using the [Link] attribute. Fora Series, the ndim attribute will always be 1 You can retrieve the number of elements in the Series using the [Link] attribute. + (Number of Bytes: You can retrieve the total number of bytes consumed by the Series data using the [Link] attribute. Example: import pandas as pd exerci camientnoatrine tA 184-406-4758 lettuce HR A-oNe eee wan series = [Link](data) shape = [Link] dimension = [Link] size=[Link] bytes = [Link] print("Shapes", shape) print(" prine("Size: size) print("Number of Bytes: nbytes) Dimer mension} Output: shape: (5,) Dimension: 1 size:s Number of Bytes: 40 Checking Emptiness: + empty: Byou can check whether a Series object is empty or not using the [Link] attribute. It returns a boolean value, True if the Series is empty and False otherwise. oxen camientntoatrine iA 184-4648 letters Aone ete ve Example: import pandas as pd empty_series = [Link]({]) nonempty_series = [Link]({10, 20, 30)) print("Empty Series:", empty_series.empty) print("Non-empty Series:", nonempty_series.empty) Output: Empty Series: True Non-empty Series: False Inthis example, empty_series |is an empty Series, so empty_series.empty| returns True . On the other hand, nonempty_series is anon-empty Series, so nonempty_series.empty returns False Checking Presence of NaNs: + QHasNans: EB you can check whether a Series object contains any NaN (missing) values using the [Link] attribute. It returns a boolean value, True if there are NaN values in the Series and False otherwise. import pandas as pd exerci camienteoatrine tA 84-4648 lettuce HR A-obe ae import numpy as np data = [10, 20, [Link], 40, [Link]} series = [Link](data) print("Has NaNs:", [Link]) Output: Has NaNs: True In this example, the Series series. contains NaN values, so |[Link] returns True Series Functions [Link] () + This function is used to map the values of a Series based on a provided mapping or a callable function. It takes a dictionary, Series, or a function as input and returns a new Series with the mapped values. Example: mport pandas as pd data =('W: ‘Apple’, 'B': ‘Banana’, 'C': ‘Cherry’} series = [Link]((A, ‘8, 'C'}) mapped_series = [Link](data), oxen camientntoatrine iA 184-4648 letters Aone ete ven print("Mapped Series:") print(mapped_series) Output: Mapped Series: 0 Apple 1 Banana 2 Cheny dtype: object @ [Link]() : Bi This function calculates the standard deviation of the values in a Series. It returns a scalar value representing the standard deviation. import pandas as pd data = [10, 20, 30, 40, 50] series = [Link](data) std= [Link]() print("Standard Deviation:", std) Output: Standard Deviation: 15.811388300841896 oxen camientntoatrine iA 184-4648 letters Aone ete an @ Series.to_frame() : This function converts a Series object to a DataFrame. It returns a new DataFrame where the Series becomes a single column, and the index of the Series becomes the DataFrame index import pandas as pd data=|10, 20, 30, 40, 50] series = [Link](data) df=series.to_frame() print("DataFrame:") print(df) @ Series.value_counts() : oxen camientntoatrine iA 184-4648 letters Aone ete an ES This function returns a Series containing counts of unique values in the original Series. The resulting Series is sorted in descending order by default. import pandas as pd data=['A;'B,'A,'C; 8; 'B'] series = [Link](data) value_counts = series.value_counts() print("Value Counts:") print(value_counts) Output: Value Counts: 83 A2 C1 dtype: inté Example import pandas as pd import numpy as np # Example 1: Mapping values using a dictionary a= [Link]({'Jave,'C; ‘C++ [Link])) oxen camientntoatrine iA 184-4648 letters Aone ete mapped_series = [Link]({‘Java': ‘Core'}) print(mapped_series) ce # Example 2: Mapping values using a format string a= [Link]({'Java', ‘C; ‘C++ [Link])) mapped_series = [Link](' like {)\format, na_action="ignore’) print{mapped_series) ype: oxjeee # Example 3: Mapping values using a format string and handling missing values a= [Link]({'Java, ‘C; ‘C++ [Link])) mapped_series = [Link]('! like {}.format) mapped_series_ignore_na=[Link]('I like ()\format, na_actio print{mapped_series) print(mapped_series_ignore_na) ‘The na_action=" ignore’ parameteris specified to handle missing values. Ittellsthe map) function to ignore any missing values [Link] )in the Series ane not perform any transformation on them, ignore’) oxen camennoatrine tlhe sO HRA o8e eee an ype: oujece Python Pandas DataFrame (A DataFrame is a two-dimensional data structure in pandas that can store and manipulate data in a tabular form. It consists of rows and columns, similar to a spreadsheet or a SQL table. There are several ways to create a DataFrame in pandas: using a dictionary: @ You can create a DataFrame by passing a dictionary of lists, arrays, or series. The keys of the dictionary represent the column names, and the values represent the data for each column. The columns must be of the same length Example: import pandas as pd data={ 'Name':[/Alice, ‘Bob, ‘Charlie’, ‘Age’: [25, 30, 35), ‘city's [New York, ‘London, ‘Paris } oxen camientntoatrine iA 184-4648 letters Aone ete aun df= [Link](data) prine(af) one age city using li You can create a DataFrame from a list of lists or alist of arrays. Each inner list or array represents a row in the DataFrame. Example: import pandas as pd data=[ Alice’, 25, 'New York’ [’Boby, 30, ‘London’ [Charlie 35, "Paris' ] df= [Link](data) print(df) df= [Link](data, columns=['Name’, ‘Age’, City']) printtdf) oxen camientntoatrine iA 184-4648 letters Aone ete df = [Link](data, columns=['Name’, ‘Age’ ‘City’ prine(df) A aitce 35. New ork Using NumPy ndarrays: @ You can create a DataFrame from a 2D NumPy ndarray. Each row in the ndarray corresponds to a row in the DataFrame. Example: import pandas as pd import numpy as np data = [Link](l [Alice’,25, ‘New York’), ['Bob; 30, ‘London’ Charlies 35, Paris’) df= [Link](data, columns=['Name’, ‘Age’, ‘City']) print(df) oxen camientntoatrine iA 184-4648 letters Aone ete ane Age city Using Series: You can create a DataFrame from a dictionary of Series. Each Series represents a column in the DataFrame. Example: import pandas as pd data={ 'Name': [Link]({Alice’, ‘Bobs 'Charlie') ‘Age’: [Link]([25, 30, 35]), ‘City’: [Link]({/New York, ‘London’, 'Paris'}) } df= [Link](data) print(af) ADataFrame is a two-dimensional data structure in pandas that can store and manipulate data in a tabular form. It consists of rows and columns, similar to a spreadsheet or a SQL table. There are several ways to create a DataFrame in pandas Usinga dictionary: oxen camientntoatrine iA 184-4648 letters Aone ete mn You can create a DataFrame by passing a dictionary of lists, arrays, or series. The keys of the dictionary represent the column names, and the values represent the data for each column. The columns must be of the same length. Using lists: You can create a DataFrame from a list of lists ora list of arrays. Each inner list or array represents a row in the DataFrame. Using NumPy ndarrays: You can create a DataFrame from 2 2D NumPy ndarray. Each row in the ndarray corresponds to a row in the DataFrame. Using Series: You can create a DataFrame from a dictionary of Series. Each Series represents a column in the DataFrame. Once you have created a DataFrame, you can perform various operations on it. Here are some common operations: + Column Selection: You can select one or more columns from a DataFrame using indexing or by specifying the column, names. + Column Addition: You can add new columns to a DataFrame by assigning values to them. The new columns can be based on existing columns or computed from them. + Column Deletion: You can delete one or more columns from a DataFrame using the del keyword or the dropl) function. + Row Selection: You can select specific rows from a DataFrame based on conditions or by specifying row indices. + Row Addition: You can add new rows to a DataFrame using the append() function or by creating a new DataFrame and concatenating it with the original DataFrame. oxen camientntoatrine iA 184-4648 letters Aone ete + Row Deletion: You can delete one or more rows from a DataFrame using the drop() function with the appropriate row indices. Here are some common operations that you can perform on a DataFrame along with examples: Column Selection: You can select one or more columns from a DataFrame using indexing or by specifying the column names. Example: import pandas as pd data= 'Name': [Alice’, "Bobs ‘Charli ‘Age [25, 30, 351, ‘City’s [New York; ‘London’, 'Paris'] } df= [Link](data) [Zt Selecting a single column nrame_column = df{'Name'] print{name_column) oxen camiennoatrine tA 84-4648 letters HR AS-oNe eee # Selecting multi name_age_columns = df{['Name;, ‘Age']] print{name_age_columns) elie 35 Column Addition: You can add new columns to a DataFrame by assigning values to them. The new columns can be based on existing columns or computed from them. import pandas as pd data 'Name': ‘Alice’, Bob’, ‘Charlie'), ‘Age’ [25, 30, 35], ‘City’: [New York, ‘London’, 'Paris'] : df= [Link](data) # Adding a new column based on an existing column df['Upper_Name'] = dff'Name'].strupper() prine(df) oxen camientntoatrine iA 184-4648 letters Aone ete ane Age city Upper sane # adding a new column with computed values. df{'Birth_Year'] = [Link]().year-df{'Age'] prine(df) fame Age City Upper sane rth Year Column Deletion: You can delete one or more columns from a DataFrame using the | del keyword or the | drop()| function. Example: import pandas as pd data ‘Name's [Alice ‘Bob, ‘Charlie'), "Age': [25, 30, 35], ‘City's 'New York; ‘London’, 'Paris'] } df= [Link](data) @ alice 35. Now York oxen camiennoatrine tA 84-4648 letters HR AS-oNe eee awn # Deleting a column using the del keyword del dff’age’] prinetdf) wane city # Deleting a column using the drop() function df= dfdrop(‘City’ axis=1) prine(df) one's Row Selectio You can select specific rows from a DataFrame based on conditions or by specifying row indices. Example: import pandas as pd data "Name's Alice, 'Bo! ‘charlie, oxen camientntoatrine iA 184-4648 letters Aone ete ‘Age’: [25, 30, 35], ‘City's [New York, ‘London, 'Paris'] } df= [Link](data) # Selecting rows based on a con adults = dffdf'Age'] >= 30] print(adults) wane age 2 bab 8 # Selecting rows by specifying row indices specific_rows = [Link]{l0, 21] print(specific_rows) (Row Addi You can add new rows to a DataFrame using the append() function or by creating @ new DataFrame and concatenating it with the original DataFrame Example: import pandas as pd data "Name's Alice, 'Bo! ‘Charlie, oxen camientntoatrine iA 184-4648 letters Aone ete wn ‘Age’: [25, 30, 35], ‘City's [New York; ‘London, 'Paris'] } df= [Link](data) # Adding a new row using append() function new_row = [Link]({'Name': ['Dave'), ‘Age’: [28], ‘City’ ['Berlin'}) df= [Link]{new_row, ignore_index=True) print(af) # adding new rows by concatenating DataFrames [Link](| {'Name': ‘Emily; ‘Age': 32, ‘City’: ‘Sydney’, {’Name': "Frank, ‘Age’: 27, ‘City’s Tokyo} ] df= [Link], new_rows], ignore_index=True) new_rows= print(af) ene tee tty 2 cnariie 35 Parse oxen camiennoatrine tA 84-4648 letters HR AS-oNe eee © ey 32 sytney Row Deletion: You can delete one or more rows from a DataFrame usingthe drop () function with the appropriate row indices. import pandas as pa f Create a sample DataFrame data= (Name's (ohn; Jane, “nge' 25, 30,35, 40} t= [Link](éata) printf ice "80, # Deleting rows using the drop() function # Drop rows with labels 0 and 2 df=dfdrop({0, 2], axis=0}# (0, 21 isthe ist ofrow labels you want to drop. In this case it specifies that you want to drop the rows with Labels 0 and 2 print(af) one ge the rows with abels (John) and 2 (Alice) are dropped, andthe resulting DataFrame contains only the remaining rows (ane) and 3 (Bob), oxen camientntoatrine iA 184-4648 letters Aone ete own DataFrame Functions Pandas [Link](): + Description: Add the rows of another DataFrame to the end of the given DataFrame. + Example: import pandas as pd afl = [Link]({’a’: [1,2, 31,8 [4,5, 61)) df2= [Link]((': [7, 8, 9], 8": [10, 11, 12])) f_appended = df1.appendidt2) print(df_appended) Pandas [Link](): + Description: Apply a function to every single value of the DataFrame. + Example: import pandas as pd df= [Link](('A’ [1, 2, 3],'8:[4, 5, 6) def square(x): return x**2 df_squared = [Link](square) osimwmerice camisetas 10464750387 ta Seba 4 eDHUEA7, print(df_squared) [Pandas [Link](): + Description: Add new columns into a DataFrame. + Example: import pandas as pd df = [Link]((’' [1, 2, 3],'3' 4,5, 6) df_new = dfassign(C-[7, 8, 9], D=[10, 11, 12)) print(df_new) [Pandas [Link](): + & Description: Cast the DataFrame to a specified dtype + Example: import pandas as pd df= [Link](('A’ (1,2, 3],'8[4,5, 6) df casted = [Link]({'s’ float}) print{df_easted) oxen camientntoatrine iA 184-4648 letters Aone ete (Pandas [Link](): + Description: Perform concatenation operation along an axis in the DataFrame. + Example: import pandas as pd df1 = [Link]({'A’: (1, 2, 31, 'B':[4, 5, 6)}) af2 = [Link]((': (7,8, 9},'8'[10, 1, 129) df_concatenated = [Link](([dfl, df2]) print(df_concatenated) [Pandas [Link](): + & Description: Count the number of non-NA cells for each column or row. + Example: import pandas as pd df= [Link](('A’: [1, 2, None}, 'B': [4, None, 6]}) column_counts = [Link]() print{column_counts) row_counts = [Link](axis=1) oxen camfennoatrine iA 7484-46458 letters HR AS-oNe eee print(row_counts) [Pandas [Link](): + @ Description: Cast the DataFrame to a specified dtype. + Example: import pandas as pd df= [Link](('A’ [1,2, 3], '8':[4,5, 61} df_casted = [Link]({'A’: float}) print(df_casted) (Pandas [Link](): + @ Description: Calculate statistical data like percentile, mean, and standard deviation of the numerical values in the DataFrame. + Example: import pandas as pd df= [Link]({'A’: (1, 2, 3, 4, 5]}) [Link]() print(description) descriptioy exerci camentoatrine tA 124-4647 8 letters HRA oNe eee [Pandas DataFrame.drop_duplicates(): + @ Description: Remove duplicate values from the DataFrame, + Example: import pandas as pd df= [Link]((’A’ [1,2,2,3,4,4,5))) df_unique=df.drop_duplicates() print(df_unique) [Pandas [Link](): + Description: Split the data into various groups based on a column or multiple columns, + Example: import pandas as pd df= [Link]({’’: ['fo0; ‘bar, ‘foo’, ‘bar’, 'foo'], 'B':['one, ‘one’, ‘two’, ‘two’, ‘one'], 'C':(1, 2, 3,4, 5))) grouped = [Link](’A) print(grouped.get_group(‘foo')) exerci camentatrine tA 84-4648 letter aOR AS-oNe eee (Pandas [Link](): + @ Description: Return the first n rows of the DataFrame based on position. + Example: import pandas as pd df= [Link]({'A’: (1,2, 3, 4, 5]}) first_three_rows = dt head(3) print(first_three_rows) (Pandas DataFrame-hist(): + @ Description: Divide the values within a numerical variable into "bins" and create a histogram. + Example: import pandas as pd import [Link] as plt df= pd,DataFrame({’A’: (1, 2, 3, 4, 5]}) df['A’]-hist() [Link]() oxen camientntoatrine iA 184-4648 letters Aone ete aun ao 15 20 25 30 35 40 45 50 (Pandas [Link](): +e Description: iterate over the rows of the DataFrame as (index, series) pairs. + Example: import pandas as pd df= [Link]((‘A’ [1, 2,3], 'B' [a for index, row in [Link](): print(f"index: {index}, Row: {row)") oxmncermctecanlntnogerine tll 4 BK4SASSB/lttshraebat 6-H A e864 te none: 2, ctype: object Pandas [Link](): + Description: Return the mean of the values for the requested axis (column or row). + Example: import pandas as pd df= [Link](('A: [1,2,3),'8': 4, 5, 6) column_mean = dfmean() print(column_mean) row_mean = [Link](axis=1) print(row_mean} (& Pandas [Link](): + @ Description: Unpivot the DataFrame from a wide format to a long format. oxen camientntoatrine iA 184-4648 letters Aone ete + Example: import pandas as pd df= [Link](('A’ [1,2, 3) print(df) "2 [4, 5, 6]}) melted_df=[Link]() print{melted_df) [Pandas [Link](): + & Description: Merge two datasets together into one based on common columns or indices. + Example: import pandas as pd left_df= [Link]({A': (1,2, 3],'8':['25'b;‘c'}) right_df= [Link]({'A’ [4, 5, 6),'C:['x, ys 27) merged_df = [Link](left_df, right_df, on='A’) print(merged_df) oxen camientntoatrine iA 184-4648 letters Aone ete Index: 1) Pandas DataFrame.pivot_table(): + Description: Aggregate data with calculations such as sum, count, average, max, and min, and create a pivot table. + Example: import pandas as pd df = [Link]((’a\':['o0}, bar, foo}, bar’, 'foo'), ‘B':[fone;, ‘one; two’, two’, ‘one'], ‘C0, 2,3,4,5)) pivot_table = df pivot_table(value: print(pivot_table) [Pandas [Link](): + Description: Filter the DataFrame based on a boolean expression, + Example: import pandas as pd df= [Link](('A’ (1,2, 3,4, 5],’B': foo} ‘bar’, "foo, ‘baz’ ‘qux’)}) filtered_df= [Link]'A > 2’) prine(filtered_df) oxen camientntoatrine iA 184-4648 letters Aone ete Pandas [Link](): + Description: Shift column values or subtract the column value with the previous row value. + Example: import pandas as pd af = [Link]((’4' [1, 2,3, 4, 5D) shifted_df = off’) shift(2) print(shifted_df) Pandas [Link](): + & Description: Sort the DataFrame based on one or more columns. + Example: import pandas as pd df = [Link](('[3, 2, 1),'8° sorted_df = df.sort_values(by-'A’) print{sorted_df) (Pandas [Link](): + Description: Return the sum of the values for the requested axis (column or row). + Example: import pandas as pd oxen camientntoatrine iA 184-4648 letters Aone ete wen df= [Link]({(’A’: (1, 2, 3),'8':[4, 5, 61) column_sum = [Link]() print(colurnn_sum) row_sum = [Link](axis=1) print(row_sum) Pandas DataFrame.to_excel(): + & Description: Export the DataFrame to an Excel file. + Example: import pandas as pd df = [Link]((A' [1, 2, 3], '3': 4,5, 6) dto_excel('[Link], index=False) (Pandas [Link](): + & Description: Transpose the index and columns of the DataFrame. + Example: import pandas as pd df= [Link]({'A’: [1,2, 3) transposed_df= dftranspose() print(transposed_df) "2 14, 5, 6]}) oxen camientntoatrine iA 184-4648 letters Aone ete Pandas [Link](): + Description: Check the DataFrame for one or more conditions and replace values where the condition is False. + Example: import pandas as pd df = [Link]((4'[1, 2,3], 8° condition = df{"'}>2 filtered_df= dtwhere(condition, other=0) prine(filtered_df) [4, 5, 6]}) merge: & The merge() function in pandas is used to combine two or more DataFrames based on a common column or index. It performs database-style join operations (e.g,, inner join, outer join, left join, right join) to combine the DataFrames. The result is a new DataFrame that contains the combined data import pandas as pd # Create the first DataFrame datal ={'Name': [ohn’, ‘Jane’ ‘Alice’, ‘Age’: (25, 30, 35)} df = [Link](datal) # Create the second DataFrame data2={'Name': Alice’ 'Bob; ‘Charlie’, ‘City’s [New York; ‘London’, 'Paris']} df2 = [Link](data2) oxen camientnoatrine tA 484-4648 cletshlaed aOR AS-oNe eee wan 4 Merge the DataFrames based on the ‘Name’ column merged_d= [Link](dft, éf2, on="Name') print(merged_df) Output Name Age City 0 Alice 35 New York sein: & The join) function in pandasis used to join two or more DataFrames horizontally based on their index or columns. It aligns the DataFrames based on the specified index or column and combines them. tis similar to merge but supports only the left join operation by default import pandas as pd 4 Create the first DataFrame datal ={'Name':['John’, Jane’ ‘Alice'), ‘Age’: [25, 30, 35]} df = [Link](datal) # Create the second DataFrame data2 = {'City': [‘New York’, ‘London’, 'Paris'}} df2= [Link](data2, index=['John’, ane’, 'Alice’)) exerci camifentoatrine tA 184-4648 letters HR AS-oNe eee san # Join the DataFrames based on the index joined_df= [Link](df2) print(joined_df) Output Name Age City 0 John 25 New York 1 Jane 30 London 2 Alice 35 Paris concatenate: The concatt) function in pandas is used to concatenate two or more DataFrames vertically or horizontally along a particular axis. It stacks the DataFrames on top of each other (vertically) or side by side (horizontally). Its useful when you want to combine DataFrames without performing any column or index-based matching import pandas as pd # Create the first DataFrame data = {Name's ohn’, ane’), ‘Age’: [25 30)) df = [Link](datal) # Create the second DataFrame data2={'Name': Alice’), ‘Age’: [35]} df2= [Link](data2) exerci camifennoatrine iA 184-46 478 letter aS HR ASoNe eee sour # Concatenate the Dataframes vertically concatenated_df= [Link]((éft, d2}) print{concatenated_df) Output Name Age 0 John 25 1 Jane 30 0 Alice 35 import pandas as pd # Create two DataFrames df= [Link]((’ [1,2,3],'8':(4,5, 60) f2=[Link]({'C':[7, 8, 9},'D':[10, 11, 12]}) # Merge example merged_df = [Link](dfl, df2, left_ot print("Merged DataFrame:") print(merged_df) print) how="inner') 5 tight_o # Join example joined_df= [Link](df, lsuffix='_left, rsuffix="_right’) print("Joined DataFrame:") oxen camientntoatrine iA 184-4648 letters Aone ete print(joined_df) print) # Concatenate example concatenated_df = [Link]([dfl, df2], axis=0) print("Concatenated DataFrame (Vertical:") print(concatenated_df) print concatenated_df= [Link]({df1, df2], axis=1) print("Concatenated DataFrame (Horizontal):") print{concatenated_df) Merged DataFrame: ABCD 014710 125811 236912 Joined DataFrame: ABCD 014710 125811 236912 oxen camientntoatrine iA 184-4648 letters Aone ete Concatenated DataFrame (Vertical): ABC D 014 NaN NaN 1.25 .NaN NaN 236 NaN NaN 0. NaN NaN 7.0 10.0 1. NaN NaN 8.0 11.0 2 NaN NaN 9.0 12.0 Concatenated DataFrame (Horizontal) ABCD 014710 1258u1 236912 Pandas Function import pandas as pd # Create a Dataframe df1 = [Link](('a:[1,2, 3], ' (7,8, 9], " 5, 6]}) (20, 11, 12}}) oxen camientntoatrine iA 184-4648 letters Aone ete # Pandas [Link]() appended_df= [Link](df2) print("Appended DataFrame:") printlappended_df) # Pandas [Link]() def square(x): return x**2 applied_df=[Link](square) print(*Applied DataFrame:") print(applied_df) # Pandas [Link]() dfLassign(C=[7, 8, 9]) print("DataFrame with new column:") print(af) # Pandas [Link]() [Link]({a' float) print("DataFrame with specified dtype:") print(af) oxen camientntoatrine iA 184-4648 letters Aone ete # Pandas [Link]() concatenated_df = [Link]([dfl, df2]) print("Concatenated DataFrame:") print{concatenated_df) # Pandas [Link]() column_count = [Link]() print(*Column Count:") print(column_count) # Pandas [Link]() description = [Link]) print("DataFrame Description:") print(description) # Pandas DataFrame.drop_duplicates() deduplicated_df=dfi.drop_duplicates() print("Deduplicated DataFrame:") print{deduplicated_df) # Pandas [Link]() grouped_df=[Link]''a’).sum0) print("Grouped DataFrame:") oxen camientntoatrine iA 184-4648 letters Aone ete print(grouped_df) # Pandas [Link]() first_two_rows = [Link](2) print("First two rows of DataFrame:' print(first_two_rows) # Pandas [Link]() dfA{/A\Lhist{bins=3) pltshow() # Pandas [Link]() for index, row in [Link](): print(f"Index: {index}, Row: {row}") # Pandas [Link]() mean_value = [Link]() print("Mean Value:") print(mean_value} # Pandas [Link]() melted_df=dflmelt() print("Melted DataFrame:") oxen camientntoatrine iA 184-4648 letters Aone ete print(melted_df) # Pandas [Link]() merged_df= [Link](dfl, df2, on='\}) print("Merged DataFrame: print(merged_df) # Pandas DataFrame.pivot_table() pivot_table = dfi.pivot_table(values='8: index: print("Pivot Table:") print(pivot_table) aggfunc='mean’) # Pandas [Link]() filtered_df=[Link](’A>2') print(*Filtered DataFrame:") print(fittered_df) # Pandas [Link]() sampled_df=[Link](n=2) print("Sampled DataFrame:") print(sampled_df) # Pandas [Link]() oxen camientntoatrine iA 184-4648 letters Aone ete shifted_df= ofa’). shift(1) print("Shifted DataFrame:") print(shifted_df) # Pandas [Link]() sorted_df = dfl.sort_values(by=" print("Sorted DataFrame:") print(sorted_df) # Pandas [Link]() column_sum = [Link]() print("Column Sum:") print(column_sum) # Pandas DataFrame.to_excel() df1.to_excel(‘output-xlsx, index=False) # Pandas [Link]() transposed_df= [Link]() print("Transposed DataFrame:") print{transposed_df) # Pandas [Link]() oxen camientntoatrine iA 184-4648 letters Aone ete condition = dfi'A'] >2 filtered_df=[Link](condition, other=0) print(*Filtered DataFrame:") print(filtered_df) # Pandas [Link]() & DataFrame-filina() import pandas asp Import numpy asnp 4 Create a DataFrame with missing values f= [Link]({n' 3, 2,[Link], "B:[5,[Link], 7,8), "C2[9,10,21,[Link]})) print Original DataFrame) print) print) Rename columns renamed_df=frename(columns=[:'Colurmnt 8 Column2;"C:"Column3') print("Renamed DataFrame:") print{renamed_af) print) 4 Fill missing values with filled_dt =f fillna(o) print{"DataFrame with filed values") print(flled_af) print) oxen camientntoatrine iA 184-4648 letters Aone ete Forward fil missing values forward_fled_df= affilia(method="fill) print("Dataframe with forward filled values:") print(forward_fited_af) Original DataFrame: ABC 01050 90 1.2.0 NaN 10.9 2 NaN 7.0 110 34.080 NON Renamed DataFrame: CColumnt Column2 Columns 19 50 90 20 NaN 10.0 NaN 70 11.0 40 80 NaN DataFrame with filed values: ABC 01.950 90 12000100 20070 1.0 3.40 80 00 DataFrame with forward filled values: ABC oxen camientntoatrine iA 184-4648 letters Aone ete 91.950 90 12050100 2207010 3.4980 119 Pandas Time Series: Pandas provides powerful tools for working with time series data. includes various functionalities for indexing, slicing, and manipulating time series data. Time series data is represented as a Series or DataFrame with a DateTimeindex, which allows easy manipulation and analysis of time-based data. Pandas Datetime: Pandas datetime module provides classes and functions for working with date and time data, The key class is Timestamp, which represents a specific moment in time. The Datetimeindex class is used to index Pandas Series or DataFrame with timestamps. Some commonly used functions and methods for working with dates and times are: + pd.to_datetime(): Converts a string or an object to a pandas Timestamp object. + pd.date_range(): dip Generates a range of dates or timestamps + [Link](): i Creates a Timestamp object from a string or numeric value. + [Link]/month/day/hour/minute/second: Extracts specific components of a datetime index. import pandas as pd # Convert a string to a pandas Timestamp date_str='2023-06-08' date = pd.to_datetime(date_str) prine(date) # Output: 2023-06-08 00:00:00 oxen camientntoatrine iA 184-4648 letters Aone ete oun # Generate a range of dates date_range= pd.date_range(start="2023-01-01', end='2023-12-31; freq='D') print(date_range) atetineteden({'202-€2-01", 2023-01-02", 2823 geanagi! | 2028-12-30") ryper'daerinesa[ns]', length36s, freq") # Create a Timestamp object timestamp = [Link]('2023-06-08 12:00:00') print([Link]) # Output: 2023 Pandas Time Offset: Pandas provides time offset aliases that represent common time intervals. These aliases can be used for time frequency conversions, resampling, and other time-related operations. Some commonly used time offsets are: + D:Day + H:Hour + Tor min: Minute + S:Second + M:Month end oxen camientntoatrine iA 184-4648 letters Aone ete + AvYear end Example usage: import pandas as pd # Create a time offset of 1 hour offset = [Link] Hourl) print(offset) # Output: # Add a time offset to a timestamp timestamp = [Link]('2023-06-08 12:00:00’) new_timestamp = timestamp + [Link](2) print{new_timestamp) # Output: 2023-06-08 14:00:00 Pandas Time Periods: Pandas time periods represent fixed-length intervals, such as a day, month, or year. Time periods can be used to aggregate and group time series data, Some commonly used functions and methods for working with time periods are + [Link](): Creates a Period object from a string or numeric value. + Gipd.period_range(): Generates a range of time periods. + @[Link](): Changes the frequency of a time period. + @Periodindex: index of Period objects. oxen camientntoatrine iA 184-4648 letters Aone ete Example usage: import pandas as pd 4 Create a Period object period = [Link]|'2023-06', freq="M") print(period) # Output: 2023-06 # Generate a range of time periods period_range=pd.period_rangetsta print(period_range) ='2023-01;, end="2023-12 freq='M') Pertncnden(("2021-80", "2en1e2', "2021-03", “2022-00, «ype pias") Pandas NumPy: Pandas and NumPy are two popular libraries in Python for data manipulation and analysis. While NumPy provides a fundamental array object for numerical computing, Pandas builds on top of NumPy to provide more advanced data structures and analysis tools. Boolean indexing is a technique used in Pandas and NumPy to select subsets of data based on boolean conditions, It allows filtering data based on specific criteria or conditions. With boolean indexing, you can create boolean masks that act as filters to select rows or columns that satisfy certain conditions. import pandas as pd oxen camentnotrine tA 184-4648 lettuce HR AS-oNe eee import numpy as np 4 Create a Pandas Series s=[Link]{1, 2,3,4,5]) # Boolean indexing with Pandas filtered_pandas = s{s> 3] print(fltered_pandas) # Create a NumPy array arr=[Link]({1, 2, 3, 4, 5]) # Boolean indexing with NumPy filtered_numpy=arrlarr> 3] prineffittered_numpy) Concatenation is the process of combining multiple data structures along a particular axis. Both Pandas and NumPy provide functions for concatenating data. Example: import pandas as pd import numpy as np oxen camientntoatrine iA 184-4648 letters Aone ete en # Concatenating DataFrames with Pandas df = [Link]((’A [1, 2, 3], 'B':[4,5, 6} df2= [Link](('A’:[7, 8,9], 'B': (20, 14, 12)}) concatenated_pandas = [Link]({dfl, d2]) print{coneatenated_pandas) # Concatenating arrays with NumPy arr1= npaarray({[1, 2), (3,4) arr2=np.arrayitt5, 61) concatenated_nump} [Link]({arri,arr2)) print(coneatenated_numpy) Pandas vs NumPy: + NumPy provides the fundamental array object that allows efficient numerical computing and mathematical operations on arrays. itis well-suited for handling large numerical datasets and performing mathematical computations. + Pandas, on the other hand, builds on top of NumPy and provides higher-level data structures like Series and DataFrame, which are more convenient for data manipulation and analysis. Pandas is designed for working with structured and tabular data, offering functionality for data cleaning, filtering, merging, reshaping, and analysis. + While NumPy focuses on numerical computing and array operations, Pandas extends NumPy's capabilities by adding data alignment, handling missing values, powerful indexing and slicing, and integration with other data formats and libraries. oxen camientntoatrine iA 184-4648 letters Aone ete + In summary, NumPy is primarily used for numerical computations and arrays, while Pandas is used for data manipulation, analysis, and working with structured data. They are often used together, with Pandas leveraging the array operations and efficient data structures provided by NumPy. Convert Pandas DataFrame to Numpy array To convert a Pandas DataFrame to a NumPy array, you can use the | values. attribute of the DataFrame. Here's an example: import pandas as pd import numpy as np # Create a sample DataFrame df= [Link]({'A’ [1,2, 3], '8':[4,5, 61) # Convert DataFrame to NumPy array array = dfvalues print(array) output & (a4) [25] 36) Inthis example, the values) attribute is used to retrieve the underlying data of the DataFrame as a NumPy array. The resulting array variable will contain the values of the DataFrame in a two-dimensional array format. oxen camifentnoatrine iA 4B cletlaedatOGU-HRAS-oNe eee on Convert Pandas DataFrame to CSV import pandas as pd # Create a sample DataFrame df= [Link](('A’ [1, 2, 3], [4, 5, 6]}) # Convert DataFrame to CSV file dfto_csv(‘[Link], index=False) # Read the CSV file to verify the data df_read= pd.read_csv(‘[Link]') print(df_read) Output AB 014 125 236 Inthis example, the to_csv() function is applied on the DataFrame df} to convert to a CSV file named ‘[Link]’. The index=False_ parameters used to exclude the index column from the CSV file exerci camienteoatrine tA 84-4648 lettuce HR A-obe ae wan You can specify other parameters such as. sep (delimiter), header | (whether to include the column names), and columns (select specific columns) according to your requirements. Make sure to provide the appropriate file path and filename when using to_csv() Python Pandas Reading Files To read various types of files using Pandas, you can use the following functions: Reading from csv! «You can use the read_csv() function to read data from a CSV file, Example: import pandas as pd # Read CSV file df= pd.read_csv('[Link]') Reading from Excel File: & To read data from an Excel file, you can use the read_excel() function. Example: import pandas as pd #Read Excel file df= pd.read_excel('data xlsx’) (Reading from JSON: @ You can use the read_json() function to read data from a JSON file. exerci camientntoatrine iA 84-4645 lettuce HR Aobe eee Example: import pandas as pd # Read JSON file df=pd.read_json(‘data json’) Reading from a SQL Database: & Pandas provides the read_sql() function to read data from a SQL database. You need to establish a connection to the database and pass the SQL query or table name. Example: import pandas as pd import sqlites # Establish a connection to the database conn = [Link](‘[Link]') #Read data from a SQL query query = 'SELECT * FROM table_name' df= pd.read_sql(query, conn) # Read data froma table table_name='table_name! df= pd.read_sq|_table(table_name, conn) oxen camientntoatrine iA 184-4648 letters Aone ete Note: The above example uses SQLite as an example database. You need to install the appropriate database driver and modify the connection parameters according to your specific database. Ensure that you have the necessary libraries installed (e.g, pandas|, xLrd , openpyxl , pyodbc |, etc.) to read files in the desired format. Nitish Mehta oxmncermctecanlntnogerine tll 4 BK4SASSB/lttshraebat 6-H A e864 te ar

You might also like