Informatics Practices
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)
Python Pandas
1 Like
Answer
(a)
2015 2016 2017 2018
Messi NaN NaN NaN NaN
Ronaldo NaN NaN NaN NaN
Neymar NaN NaN NaN NaN
Hazard NaN NaN NaN NaN
Working
The DataFrame df
is created with defined columns and index labels. It is then printed, showing NaN values since no data is provided. Afterwards, df
is written to a CSV file named one.csv
.
The contents of the file one.csv
are as follows:
,2015,2016,2017,2018
Messi,,,,
Ronaldo,,,,
Neymar,,,,
Hazard,,,,
(b)
2015 2016 2017 2018
Messi 12 NaN NaN NaN
Ronaldo NaN 11 NaN NaN
Neymar NaN NaN 8 NaN
Hazard NaN NaN NaN 16
Working
The code assigns values to specific cells in the DataFrame df
, such as setting the value of 'Messi' in the '2015' column to 12 and so on. After printing the modified DataFrame, it is saved to a CSV file named two.csv
using '@' as the separator.
The contents of the file two.csv
are as follows:
@2015@2016@2017@2018
Messi@12@@@
Ronaldo@@11@@
Neymar@@@8@
Hazard@@@@16
(c)
2015 2016 2017 2018
Messi NaN NaN NaN NaN
Ronaldo NaN NaN NaN NaN
Neymar NaN NaN NaN NaN
Hazard NaN NaN NaN NaN
Working
The code reads the CSV file named one.csv
into a new DataFrame new_df
. The parameter index_col = 0
means the first column of the CSV file will be used as the index. It then prints the contents of new_df
. Since the CSV file has an empty DataFrame, the new_df
DataFrame will be empty as well.
The contents of the file one.csv
are as follows:
,2015,2016,2017,2018
Messi,,,,
Ronaldo,,,,
Neymar,,,,
Hazard,,,,
(d)
Unnamed: 0 2015 2016 2017 2018
0 Messi NaN NaN NaN NaN
1 Ronaldo NaN NaN NaN NaN
2 Neymar NaN NaN NaN NaN
3 Hazard NaN NaN NaN NaN
Working
The code used the pandas read_csv()
function to extract data from one.csv
file. The retrieved data is then stored in a DataFrame called new_df
and then it is printed. Since the index_col
parameter is not specified, the DataFrame is using default numerical indexes. The DataFrame displays "Unnamed: 0" as the header for the first column because the CSV file doesn't have a header row.
The contents of the file one.csv
are as follows:
,2015,2016,2017,2018
Messi,,,,
Ronaldo,,,,
Neymar,,,,
Hazard,,,,
(e)
@2015@2016@2017@2018
0 Messi@12@@@
1 Ronaldo@@11@@
2 Neymar@@@8@
3 Hazard@@@@16
Working
The code used the pandas read_csv()
function to extract data from the two.csv
file. The retrieved data is then stored in a DataFrame called new_df
and printed. However, if the actual separator in the two.csv
file is "@" instead of the default comma (,), then the code without specifying the sep
parameter, will not parse the data correctly. It will assume that the data is comma-separated and will not display the correct structure of the data in the DataFrame.
The contents of the file two.csv
are as follows:
@2015@2016@2017@2018
Messi@12@@@
Ronaldo@@11@@
Neymar@@@8@
Hazard@@@@16
(f)
Unnamed: 0 2015 2016 2017 2018
0 Messi 12.0 NaN NaN NaN
1 Ronaldo NaN 11.0 NaN NaN
2 Neymar NaN NaN 8.0 NaN
3 Hazard NaN NaN NaN 16.0
Working
The code extracts data from the two.csv
file using the read_csv()
function and assigns it to the new_df
DataFrame. It then prints new_df
. The sep
parameter is used to ensure that the DataFrame is correctly parsed, and default indexes are used since index_col
is not specified. The term 'Unnamed' is used because a header for the first column is not mentioned in the CSV file.
The contents of the file two.csv
are as follows:
@2015@2016@2017@2018
Messi@12@@@
Ronaldo@@11@@
Neymar@@@8@
Hazard@@@@16
Answered By
2 Likes
Related Questions
What additional argument do you need to specify in to_sql() so that old data of MySQL table is retained ?
If query is a string storing an SQL statement. Write statements so that the data is fetched based on query from SQL database Mydata.
Are the following two statements same ? Why/Why not ?
(i)
pd.read_csv('zoo.csv', sep = ',')
(ii)
pd.read_csv('zoo.csv')
How are following two codes similar or different ? What output will they produce ?
(i)
df = pd.read_csv("data.csv", nrows = 5) print(df)
(ii)
df = pd.read_csv("data.csv") print(df)