Python Pandas Flashcards
Define the Pandas/Python pandas?
Pandas is defined as an open-source library that provides high-performance data manipulation in Python.
The name of Pandas is derived from the word Panel Data, which means an Econometrics from Multidimensional data. It can be used for data analysis in Python and developed by Wes McKinney in 2008.
Mention the different types of Data Structures in Pandas?
Pandas provide two data structures, which are supported by the pandas library, Series, and DataFrames. Both of these data structures are built on top of the NumPy.
A Series is a one-dimensional data structure in pandas, whereas the DataFrame is the two-dimensional data structure in pandas.
Define Series in Pandas?
A Series is defined as a one-dimensional array that is capable of storing various data types. The row labels of series are called the index. By using a ‘series’ method, we can easily convert the list, tuple, and dictionary into series. A Series cannot contain multiple columns.
How can we calculate the standard deviation from the Series?
The Pandas std() is defined as a function for calculating the standard deviation of the given set of numbers, DataFrame, column, and rows.
Series.std(axis=None, skipna=None, level=None, ddof=1, numeric_only=None, **kwargs)
Define DataFrame in Pandas?
A DataFrame is a widely used data structure of pandas and works with a two-dimensional array with labeled axes (rows and columns) DataFrame is defined as a standard way to store data and has two different indexes, i.e., row index and column index. It consists of the following properties:
The columns can be heterogeneous types like int and bool.
It can be seen as a dictionary of Series structure where both the rows and columns are indexed. It is denoted as “columns” in the case of columns and “index” in case of rows.
What are the significant features of the pandas Library?
The key features of the panda’s library are as follows:
Memory Efficient Data Alignment Reshaping Merge and join Time Series
Explain Reindexing in pandas?
df.reindex()
Reindexing is used to conform DataFrame to a new index with optional filling logic. It places NA/NaN in that location where the values are not present in the previous index. It returns a new object unless the new index is produced as equivalent to the current one, and the value of copy becomes False. It is used to change the index of the rows and columns of the DataFrame.
What is the name of Pandas library tools used to create a scatter plot matrix?
Scatter_matrix
Define the different ways a DataFrame can be created in pandas?
We can create a DataFrame using following ways:
Lists
Dict of ndarrays
Example-1: Create a DataFrame using List:
import pandas as pd # a list of strings a = ['Python', 'Pandas'] # Calling DataFrame constructor on list info = pd.DataFrame(a) print(info)
Output:
0 0 Python 1 Pandas Example-2: Create a DataFrame from dict of ndarrays:
import pandas as pd
info = {‘ID’ :[101, 102, 103],’Department’ :[‘B.Sc’,’B.Tech’,’M.Tech’,]}
info = pd.DataFrame(info)
print (info)
Output:
ID Department 0 101 B.Sc 1 102 B.Tech 2 103 M.Tech
Explain Categorical data in Pandas?
A Categorical data is defined as a Pandas data type that corresponds to a categorical variable in statistics. A categorical variable is generally used to take a limited and usually fixed number of possible values. Examples: gender, country affiliation, blood type, social class, observation time, or rating via Likert scales. All values of categorical data are either in categories or np.nan.
This data type is useful in the following cases:
It is useful for a string variable that consists of only a few different values. If we want to save some memory, we can convert a string variable to a categorical variable.
It is useful for the lexical order of a variable that is not the same as the logical order (?one?, ?two?, ?three?) By converting into a categorical and specify an order on the categories, sorting and min/max is responsible for using the logical order instead of the lexical order.
It is useful as a signal to other Python libraries because this column should be treated as a categorical variable.
How will you create a series from dict in Pandas?
A Series is defined as a one-dimensional array that is capable of storing various data types.
We can create a Pandas Series from Dictionary:
Create a Series from dict:
We can also create a Series from dict. If the dictionary object is being passed as an input and the index is not specified, then the dictionary keys are taken in a sorted order to construct the index.
If index is passed, then values correspond to a particular label in the index will be extracted from the dictionary.
info = {'x' : 0., 'y' : 1., 'z' : 2.} a = pd.Series(info) print (a)
Output:
x 0.0
y 1.0
z 2.0
dtype: float64
How can we create a copy of the series in Pandas?
.copy()
some_series.copy(deep=True)
How will you create an empty DataFrame in Pandas?
importing the pandas library
info = pd.DataFrame()
How will you add a column to a pandas DataFrame?
importing the pandas library
info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']), 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
info = pd.DataFrame(info)
print (“Add new column by passing series”)
info[‘three’]=pd.Series([20,40,60],index=[‘a’,’b’,’c’])
print (info)
print (“Add new column using existing DataFrame columns”)
info[‘four’]=info[‘one’]+info[‘three’]
How to Rename the Index or Columns of a Pandas DataFrame?
You can use the .rename method to give different values to the columns or the index values of DataFrame.
How to iterate over a Pandas DataFrame?
You can iterate over the rows of the DataFrame by using for loop in combination with an iterrows() call on the DataFrame.
for index, row in df.iterrows():
How to get the items not common to both series A and series B?
We get all the items of p1 and p2 not common to both using below example:
import pandas as pd import numpy as np p1 = pd.Series([2, 4, 6, 8, 10]) p2 = pd.Series([8, 10, 12, 14, 16]) p1[~p1.isin(p2)] p_u[~p_u.isin(p_i)]
Output:
0 2 1 4 2 6 5 12 6 14 7 16 dtype: int64
How to get frequency counts of unique items of a series?
p= pd.Series(np.take(list('pqrstu'), np.random.randint(6, size=17))) p.value_counts()
It uses np.random.randint(6, size=17) to generate an array of 17 random integers between 0 and 5 (inclusive).
The np.take(list(‘pqrstu’), …) part maps these integers to the characters ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, and ‘u’.
So, p contains a random sequence of these characters.
Output:
s 4 r 4 q 3 p 3 u 3
How to convert a numpy array to a dataframe of given shape?
Input
We can reshape the series p into a dataframe with 6 rows and 2 columns as below example:
p = pd.Series(np.random.randint(1, 7, 35)) p = pd.Series(np.random.randint(1, 7, 35)) info = pd.DataFrame(p.values.reshape(7,5)) print(info)
Output:
0 1 2 3 4 0 3 2 5 5 1 1 3 2 5 5 5 2 1 3 1 2 6 3 1 1 1 2 2 4 3 5 3 3 3 5 2 5 3 6 4 6 3 6 6 6 5
How can we convert a Series to DataFrame?
The Pandas Series.to_frame() function is used to convert the series object to the DataFrame.
Series.to_frame(name=None)
name: Refers to the object. Its Default value is None. If it has one value, the passed name will be substituted for the series name.
s = pd.Series([“a”, “b”, “c”],
name=”vals”)
s.to_frame()
Output:
vals 0 a 1 b 2 c
What is Pandas NumPy array?
Numerical Python (Numpy) is defined as a Python package used for performing the various numerical computations and processing of the multidimensional and single-dimensional array elements. The calculations using Numpy arrays are faster than the normal Python array.
How can we convert DataFrame into a NumPy array?
For performing some high-level mathematical functions, we can convert Pandas DataFrame to numpy arrays. It uses the DataFrame.to_numpy() function.
The DataFrame.to_numpy() function is applied to the DataFrame that returns the numpy ndarray.
DataFrame.to_numpy(dtype=None, copy=False)
How can we convert DataFrame into an excel file?
We can export the DataFrame to the excel file by using the to_excel() function.
To write a single object to the excel file, we have to specify the target file name. If we want to write to multiple sheets, we need to create an ExcelWriter object with target filename and also need to specify the sheet in the file in which we have to write.
How can we sort the DataFrame?
We can efficiently perform sorting in the DataFrame through different kinds:
By label
By Actual value
By label
The DataFrame can be sorted by using the sort_index() method. It can be done by passing the axis arguments and the order of sorting. The sorting is done on row labels in ascending order by default.
By Actual Value
It is another kind through which sorting can be performed in the DataFrame. Like index sorting, sort_values() is a method for sorting the values.
It also provides a feature in which we can specify the column name of the DataFrame with which values are to be sorted. It is done by passing the ‘by’ argument.
What is Time Series in Pandas?
The Time series data is defined as an essential source for information that provides a strategy that is used in various businesses. From a conventional finance industry to the education industry, it consists of a lot of details about the time.
Time series forecasting is the machine learning modeling that deals with the Time Series data for predicting future values through Time Series modeling.
What is Time Offset?
The offset specifies a set of dates that conform to the DateOffset. We can create the DateOffsets to move the dates forward to valid dates.
Define Time Periods?
The Time Periods represent the time span, e.g., days, years, quarter or month, etc. It is defined as a class that allows us to convert the frequency to the periods.