KnowledgeBoat Logo
|

Informatics Practices

What is the output of the following code ?

d = {'col1': [1, 4, 3 ], 'col2': [6, 7, 8], 'col3': [9, 0, 1]}
df = pd.DataFrame(d)
print("Original DataFrame")
print(df)
print("New DataFrame :")
dfn = df.drop(df.index[[1, 2]])
print(dfn)

Python Pandas

2 Likes

Answer

Original DataFrame
   col1  col2  col3
0     1     6     9
1     4     7     0
2     3     8     1
New DataFrame :
   col1  col2  col3
0     1     6     9

Working

The code creates a DataFrame using the pandas library in Python, named df, with three columns ('col1', 'col2', 'col3') and three rows of data. The DataFrame df is printed, and then a new DataFrame named dfn is created by dropping the rows with indices 1 and 2 from the original DataFrame using df.drop(df.index[[1, 2]]). The resulting DataFrame, dfn, contains only the first row from the df DataFrame, removing rows 2 and 3.

Answered By

2 Likes


Related Questions