Page 80 - Informatics_Practices_Fliipbook_Class12
P. 80
Series method unique() returns the distinct elements in a column. The unique values are returned in the
Ø
order in which they appear in the original DataFrame column and the missing values (NaN) are ignored in the
output.
The method value_counts() returns the count of occurrences of each unique value in a column of the
Ø
dataframe.
Pandas method pd.concat() allow us to concatenate a DataFrame with the another DataFrame.
Ø
Pandas method drop() can be used to drop a column or row from the DataFrame.
Ø
The rename() method in Pandas is used to rename columns. It allows us to specify new names for one
Ø
or more columns using a dictionary. The syntax for using the rename() method to rename columns is as
follows:
df.rename(columns={'current_name': 'new_name'}, inplace=True)
We may also rename columns in a Pandas DataFrame by specifying the complete list of names of the columns,
Ø
retaining some of the column names as it is while modifying some others by assigning it to attribute columns
of groceryDF.
We can save the DataFrame object as CSV (Comma-Separated Values) file using the to_csv() function of
Ø
Pandas DataFrame:
df.to_csv('output.csv', index=False)
We can organise the data in a DataFrame into groups using the function groupby(), based on
Ø
specific criteria, such as values stored in one or more columns. The method returns a DataFrameGroupBy
object.
We can apply more than one aggregate operations by passing the list of operations to be carried out on the
Ø
groups of data as an argument to the function agg.
Solved Exercise
A. Multiple Choice Questions
1. Which of the following methods can be used to create a Pandas DataFrame from the following dictionary:
details = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]}?
a. df = pd.DataFrame(details)
b. df = pd.DataFrame.from_dict(details)
c. df = pd.DataFrame.from_dictlist(details)
d. df = pd.DataFrame.from_series(details)
2. Consider the following Pandas Series:
s = pd.Series([10, 20, 30, 40])
Which of the following method can be used to create a DataFrame with the series as a column named 'Prices'?
a. df = pd.DataFrame({'Prices': s})
b. df = pd.DataFrame(s, columns=['Prices'])
c. df = pd.DataFrame.from_series({'Prices': s})
d. df = pd.DataFrame.from_dict({'Prices': s})
3. Which of the following is the correct way to read a CSV file in the Pandas DataFrame?
a. df = pd.read_csv('details.csv') b. df = pd.load_csv('details.csv')
c. df = pd.create_dataframe('details.csv') d. df = pd.read_data('details.csv')
66 Touchpad Informatics Practices-XII

