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
Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 above. Predict the output produced by following code fragment :
Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 above. Predict the output produced by following code fragment :
print(df2.loc["Jiya"]) print(df2.loc["Jiya", "IQ"]) print(df2.loc["Jiya":"Tim", "IQ":"College"]) print(df2.iloc[0]) print(df2.iloc[0, 5]) print(df2.iloc[0:2, 5:8])What is the output of the following code ?
data = {'age': [20, 23, 22], 'name': ['Ruhi', 'Ali', 'Sam']} df1 = pd.DataFrame(data, index=[1, 2, 3]) print("Before") print(df1) df1['Edu'] = ['BA', 'BE' , 'MBA'] print('After') print(dfl)Consider the given DataFrame 'Genre' :
No Type Code 0 Fiction F 1 Non-fiction NF 2 Drama D 3 Poetry P Write suitable Python statements for the following :
(i) Add a column called Num_Copies with the following data : [300, 290, 450, 760].
(ii) Add a new genre of type 'Folk Tale' having code as "FT" and 600 number of copies.
(iii) Rename the column 'Code' to 'Book_Code'.