Informatics Practices

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)
                               

Python Pandas

1 Like

Answer

The two codes are similar in that they both use pd.read_csv() to read a CSV file named 'data.csv' into a pandas DataFrame df. However, they differ in their usage of the nrows parameter. The first code uses the nrows parameter with a value of 5, indicating that it reads only the first 5 rows of the CSV file. On the other hand, the second code does not have the nrows parameter, so it reads the entire CSV file.

For code (i), the output will be a DataFrame containing the first 5 rows of the 'data.csv' file. For code (ii), the output will be a DataFrame containing all the rows of the 'data.csv' file.

Answered By

1 Like


Related Questions