rows.
import pandas list1 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]] df1 = pandas.DataFrame(list1) print("df1: \n", df1) df1.drop([0, 2], axis=0, inplace=True) df1.drop([1, 2], axis=1, inplace=True) print("df1 after dropping rows and columns: \n", df1) df1.reset_index(drop=True, inplace=True) print("df1 after resetting indexes of rows: \n", df1)
Here, the parameter drop=True indicates that the old indexes won’t be stored in the DataFrame as a new column. Instead, the old indexes will be dropped completely. And the parameter inplace=True indicates that we are not creating any new DataFrame after resetting the indexes of the rows.
The output of the above program will be:
df1: 0 1 2 3 4 0 1 2 3 4 5 1 6 7 8 9 10 2 11 12 13 14 15 3 16 17 18 19 20 df1 after dropping rows and columns: 0 3 4 1 6 9 10 3 16 19 20 df1 after resetting indexes of rows: 0 3 4 0 6 9 10 1 16 19 20
How to reset the indexes of columns of a DataFrame using the pandas Python library?
In the pandas Python library, there is no direct way of resetting the indexes of the columns. Instead, we can first take a transpose of the DataFrame. After that we can reset the indexes of the rows in the transposed DataFrame. And then, we can take …






0 Comments