Pandas reset_index(), sample(), set_axis(), set_index(), take(), truncate()

The Pandas DataFrame has several Re-indexing/Selection/Label Manipulations methods. When applied to a DataFrame, these methods evaluate, modify the elements and return the results.


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame reset_index()

The reset_index() method resets the DataFrames index and reverts the DataFrame to the default (original) index.

The syntax for this method is as follows:

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
ParameterDescription
levelThis parameter can be an integer, string, tuple, or list-like. It removes said levels from the index. By default, this parameter removes all levels.
dropDo not insert an index into a DataFrame column. This option will reset the index to the original integer index.
col_levelIf multi-level, this parameter determines the insertion level. By default, use the first level.

For this example, we have three (3) Classical Composers with some details about their life. They will be assigned levels based on the difficulty of their compositions.

Code – reset_index()

data = {'Composer':  ['Chopin', 'Listz', 'Haydn'],
        'Born':      [1810, 1811, 1732],
         'Country':  ['France', 'Austria', 'Austria']}

index = {'Level-1', 'Level-2', 'Level-3'}
df = pd.DataFrame(data, index)
print(df)

df.reset_index(inplace=True, drop=True)
print(df)
  • Line [1] creates a dictionary of lists and saves it to data.
  • Line [2] sets index labels for the Composers and saves them to the variable index.
  • Line [3] creates a DataFrame and assigns it to df.
  • Line [4] outputs the result to the terminal.
  • Line [5] resets the DataFrame index (reset_index()) back to the original integer index.
  • Line [6] outputs the result to the terminal.

Output

df
 Composer Born Country
Level-1Chopin 1810  France
Level-3   Listz 1811 Austria
Level-2   Haydn 1732 Austria
result
 Composer Born Country
0Chopin 1810  France
1   Listz 1811 Austria
2   Haydn 1732 Austria

Another way to accomplish the above task is to use the concat() method.

Code – concat()

data = {'Composer':  ['Chopin', 'Listz', 'Haydn'],
        'Born':      [1810, 1811, 1732],
        'Country':   ['France', 'Austria', 'Austria']}

index = {'Level-1', 'Level-2', 'Level-3'}
df = pd.DataFrame(data, index)
print(df)

df1 = pd.concat([df], ignore_index=True)
print(df)
  • Line [1] creates a dictionary of lists and saves it to data.
  • Line [2] sets index labels for the Composers and saves them to the variable index.
  • Line [3] creates a DataFrame and assigns it to df.
  • Line [4] outputs the result to the terminal.
  • Line [5] resets the DataFrame index (concat()) back to the original integer index.
  • Line [6] outputs the result to the terminal.

Output

df
 Composer Born Country
Level-1Chopin 1810  France
Level-3   Listz 1811 Austria
Level-2   Haydn 1732 Austria
result
 Composer Born Country
0Chopin 1810  France
1   Listz 1811 Austria
2   Haydn 1732 Austria

DataFrame sample()

The sample() method retrieves and returns a random sample of columns or rows (depending on the selected axis) from a DataFrame/Series.

The syntax for this method is as follows:

DataFrame.sample(n=None, frac=None, replace=False, weights=None, 
                 random_state=None, axis=None, ignore_index=False)
ParameterDescription
nN is the number of elements (items) to return from the selected axis. By default, one (1).
fracThe fraction of elements (items) to return from the selected axis. If frac, do not use the N parameter. If the value of frac is more than one (1) replace parameter must be True.
replaceIf True, allow a sample of the same row more than once. If False, do not allow the same row more than once. By default, False.
weightsIf None, weight is set to equal probability weighting. If a Series, it will align with the object on the index. If not located, ignore the index: assign the weights zero (0). If a DataFrame, accept the column name when the selected axis is zero (0).
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
ignore_indexIf True, the index will start numbering from 0 on (ex: 0, 1, 2, etc.).

For these examples, the finxters.csv data saves to a DataFrame to manipulate the data.

df = pd.read_csv('finxters.csv')
result = df['First_Name'].sample(n=3, random_state=1)
print(result)
  • Line [1] reads in the comma-separated CSV file and saves it to df.
  • Line [2] retrieves three (3) random ‘First_Names‘ values and saves them to the result variable.
  • Line [3] outputs the result to the terminal.

Output

27Victoria
35Diana
40Owen
Name: First_Name, dtype: object

In this example, the np.random.randint() method calls and generates random integers on a selected column.

Code – Example 2

df   = pd.read_csv('finxters.csv')
nums = np.random.randint(df['FID'], size=50)
result = df['FID'].sample(n=3, random_state=nums)
print(result)
  • Line [1] reads in the comma-separated CSV file and saves it to df.
  • Line [2] generates random integers (np.random.randint()) from the CSV file based on the ‘FID‘ column.
  • Line [3] retrieves three (3) integers from the random numbers generated on Line [2]. This output saves to the result variable.
  • Line [4] outputs the result to the terminal.

Output

343002381
153002244
173002260
Name: FID, dtype: int64

DataFrame set_axis()

The set_axis() method assigns index(es) to the selected axis.

The syntax for this method is as follows:

DataFrame.set_axis(labels, axis=0, inplace=False)
ParameterDescription
labelsThis parameter is a list or a list-like object containing index labels.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
inplaceIf False, a copy of the original DataFrame/Series is updated. This parameter is None, by default.

For these examples, the index saves to the selected axis.

In this example, we set the axis to the row index.

Code – Example 1

df = pd.DataFrame({'Micah': [123, 120, 144], 
                   'Paula': [129, 125, 90],
                   'Chloe': [101, 95,  124]})
print(df)
				   
result = df.set_axis(['Day-1', 'Day-2', 'Day-3'], axis='index')
print(result)
  • Line [1] creates a dictionary of lists and saves it to df.
  • Line [2] outputs the DataFrame (df) to the terminal.
  • Line [3] sets the new axis for the DataFrame and saves it to the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 MicahPaulaChloe
0123129101
112012595
214490124
result
 MicahPaulaChloe
Day-1123129101
Day-212012595
Day-314490124

In this example, we set the axis to the column index.

Code – Example 2

df = pd.DataFrame({'Micah': [123, 120, 144], 
                   'Paula': [129, 125, 90],
                   'Chloe': [101, 95,  124]})
print(df)
				   
result = df.set_axis(['Micah M', 'Paula D', 'Chloe J'], axis='columns')
print(result)
  • Line [1] creates a dictionary of lists and saves it to df.
  • Line [2] outputs the DataFrame (df) to the terminal.
  • Line [3] sets the new axis for the DataFrame and saves it to the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 MicahPaulaChloe
0123129101
112012595
214490124
result
 Micah MPaula DChloe J
0123129101
112012595
214490124

DataFrame set_index()

The set_index() method sets the DataFrame index using existing columns/rows.

The syntax for this method is as follows:

DataFrame.set_index(keys, drop=True, append=False, 
                    inplace=False, verify_integrity=False)
ParameterDescription
keysA single column or list-like array. Must be the same length as DataFrame.
dropDo not insert an index into a DataFrame.
appendIf True, append columns to index. If False, do not append. By default, True.
inplaceIf True, the original DataFrame is updated. If False, a new object is updated and returned.
verify_integrityThis parameter checks the new index for duplicates (columns). Set to False for faster performance.

For this example, the Salesperson(s) who sold the highest number of cars over four (4) months display.

df = pd.DataFrame({'Salesman':    ['Greg', 'Fred', 'Helen', 'Tim'],
                   'Month':         ['Jan', 'Feb', 'Mar', 'Apr'],
                   'Sold':             [165, 156, 196, 124]})

result = df.set_index('Salesperson')
print(result)
  • Line [1] creates a Dictionary of Lists and saves it to df.
  • Line [2] sets the index to ‘Salesperson’ and saves it to the result variable.
  • Line [3] outputs the result to the terminal.

Output

 Month Sold
Salesperson
GregJan165
FredFeb156
HelenMar196
TimApr124

DataFrame take()

The take() method returns the elements (data) across the selected axis. The indexing performs on the actual position of the DataFrame element.

πŸ›‘ Note: This method has been deprecated (since version 1.0.0).

The syntax for this method is as follows:

DataFrame.take(indices, axis=0, is_copy=None, **kwargs)
ParameterDescription
indicesList (array) of integers that specify locations to take.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
is_copyAs of pandas v1.0, this parameter always returns a copy.
**kwargsTo be compatible with numpy.take(), the take() method does not affect the output.

For this example, the finxters.csv data saves to a DataFrame to manipulate the data.

df   = pd.read_csv('finxters.csv')
result = df.take([30, 31], axis=0)
print(result)
  • Line [1] reads in the comma-separated CSV file and saves it to df.
  • Line [2] takes the 30th and 31st row of the CSV file and saves it to the result variable.
  • Line [3] outputs the result to the terminal.

Output

DataFrame truncate()

The truncate() method truncates a DataFrame/Series before and after a selected index value.

The syntax for this method is as follows:

DataFrame.truncate(before=None, after=None, axis=None, copy=True)
ParameterDescription
beforeTruncate (remove) rows before a said index value. The data type can be a date, string, or integer.  
afterTruncate (remove) rows after a said index value. The data type can be a date, string, or integer.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
copyIf True, a copy of the truncated DataFrame/Series returns. This boolean is True by default.

For this example, we have a DataFrame containing a message.

df = pd.DataFrame({'C': ['f', 'i', 'n', 'x', 't', 'e', 'r'],
                   'O': ['p', 'u', 'z', 'z', 'l', 'e', 's'],
                   'D': ['a', 'w', 'e', 's', 'o', 'm', 'e'],
                   'E': ['w', 'a', 'y', '-', 't', 'o', '-'],
                   'R': ['l', 'e', 'r', 'n', '!', '!', '!']},
                   index=[1, 2, 3, 4, 5, 6, 7])
print(df)

result = df.truncate(before=2, after=4)                  
print(result)
  • Line [1] creates a DataFrame from a dictionary of lists and saves it to df.
  • Line [2] outputs the result to the terminal.
  • Line [3] truncates and saves the output to the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
result

Further Learning Resources

This is Part 10 of the DataFrame method series.

  • Part 1 focuses on the DataFrame methods abs(), all(), any(), clip(), corr(), and corrwith().
  • Part 2 focuses on the DataFrame methods count(), cov(), cummax(), cummin(), cumprod(), cumsum().
  • Part 3 focuses on the DataFrame methods describe(), diff(), eval(), kurtosis().
  • Part 4 focuses on the DataFrame methods mad(), min(), max(), mean(), median(), and mode().
  • Part 5 focuses on the DataFrame methods pct_change(), quantile(), rank(), round(), prod(), and product().
  • Part 6 focuses on the DataFrame methods add_prefix(), add_suffix(), and align().
  • Part 7 focuses on the DataFrame methods at_time(), between_time(), drop(), drop_duplicates() and duplicated().
  • Part 8 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 9 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 10 focuses on the DataFrame methods reset_index(), sample(), set_axis(), set_index(), take(), and truncate()
  • Part 11 focuses on the DataFrame methods backfill(), bfill(), fillna(), dropna(), and interpolate()
  • Part 12 focuses on the DataFrame methods isna(), isnull(), notna(), notnull(), pad() and replace()
  • Part 13 focuses on the DataFrame methods drop_level(), pivot(), pivot_table(), reorder_levels(), sort_values() and sort_index()
  • Part 14 focuses on the DataFrame methods nlargest(), nsmallest(), swap_level(), stack(), unstack() and swap_axes()
  • Part 15 focuses on the DataFrame methods melt(), explode(), squeeze(), to_xarray(), t() and transpose()
  • Part 16 focuses on the DataFrame methods append(), assign(), compare(), join(), merge() and update()
  • Part 17 focuses on the DataFrame methods asfreq(), asof(), shift(), slice_shift(), tshift(), first_valid_index(), and last_valid_index()
  • Part 18 focuses on the DataFrame methods resample(), to_period(), to_timestamp(), tz_localize(), and tz_convert()
  • Part 19 focuses on the visualization aspect of DataFrames and Series via plotting, such as plot(), and plot.area().
  • Part 20 focuses on continuing the visualization aspect of DataFrames and Series via plotting such as hexbin, hist, pie, and scatter plots.
  • Part 21 focuses on the serialization and conversion methods from_dict(), to_dict(), from_records(), to_records(), to_json(), and to_pickles().
  • Part 22 focuses on the serialization and conversion methods to_clipboard(), to_html(), to_sql(), to_csv(), and to_excel().
  • Part 23 focuses on the serialization and conversion methods to_markdown(), to_stata(), to_hdf(), to_latex(), to_xml().
  • Part 24 focuses on the serialization and conversion methods to_parquet(), to_feather(), to_string(), Styler.
  • Part 25 focuses on the serialization and conversion methods to_bgq() and to_coo().

Also, have a look at the Pandas DataFrame methods cheat sheet!

Learn Pandas the Fun Way by Solving Code Puzzles

If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?

Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.