Q. Predict the output of following code fragments one by one. For every next code fragment, consider that the changes by previous code fragment are in place. That is, for code fragment (b), changes made by code fragment (a) are persisting; for (c), changes by (a) and (b) are persisting and so on.


(a)

import pandas as pd

columns=['2015', '2016', '2017', '2018']

index=['Messi', 'Ronaldo', 'Neymar', 'Hazard']

df = pd.DataFrame (columns = columns, index = index)

print (df)

df.to_csv("c:\one.csv")


(b)

df[ '2015' ][ 'Messi'] = 12

df['2016']['Ronaldo'] = 11

df[ '2017']['Neymar'] = 8

df['2018']['Hazard'] = 16

print (df)

df.to_csv("c:\two.csv", sep= '@')


(c)

new_df = pd.read_csv('c:\one.csv',index_col = 0)

print(new_df)


(d)

new_df = pd.read_csv('c:\one.csv')

print(new_df)


(e)

new_df = pd.read_csv('c:\two.csv')

print(new_df)


(f)

new_df = pd.read_csv('c:\two.csv', sep= '@')

print(new_df)


Answer =

(a)

Output :-

       2015 2016 2017 2018
messi    NaN  NaN  NaN  NaN
ronaldo  NaN  NaN  NaN  NaN
neymar   NaN  NaN  NaN  NaN
hazard   NaN  NaN  NaN  NaN

This code Fragment creates a Dataframe df
And saving the dataframe data into csv file

(b)

Output:-
    
                   2015 2016 2017 2018
messi     12  NaN  NaN  NaN
ronaldo  NaN   11  NaN  NaN
neymar   NaN  NaN    8  NaN
hazard   NaN  NaN  NaN   16

This code Fragment inserting Some values to  Dataframe df
And saving the dataframe data into csv file with separator ‘@’

(c)

Output:-
     
This code Fragment creatin new   Dataframe  from csv file

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post