Read the first few lines specified by the CSV file using the implementation pandas

Source: Internet
Author: User
Tags pandas read csv
Below for you to share an article using the implementation pandas read CSV file specified the first few lines, with a good reference value, I hope to be helpful to everyone. Come and see it together.

CSV file for storing data sometimes the amount of data is huge, but sometimes we don't need all the data, we just need a few lines ahead.

This enables the ability to read by specifying the number of rows in Read_csv in pandas.

For example, there are data.csv files, the contents of the file are as follows:

Greydemac-mini:chapter06 greyzhang$ cat Data.csv, name_01,coment_01,,,, 2,name_02,coment_02,,,, 3,name_03,coment_03, ,,, 4,name_04,coment_04,,,, 5,name_05,coment_05,,,, 6,name_06,coment_06,,,, 7,name_07,coment_07,,,, 8,name_08, coment_08,,,, 9,name_09,coment_09,,,, 10,name_10,coment_10,,,, 11,name_11,coment_11,,,, 12,name_12,coment_12,,,, 13 , name_13,coment_13,,,, 14,name_14,coment_14,,,, 15,name_15,coment_15,,,, 16,name_16,coment_16,,,, 17,name_17, Coment_17,,,, 18,name_18,coment_18,,,, 19,name_19,coment_19,,,, 20,name_20,coment_20,,,, 21,name_21,coment_21,,,,

If the data we need is only the first 5 rows, then the reading can be specified in nrows way. Write the following code:

1 #!/usr/bin/python 2  3 Import pandasas PD 4  5 data = pd.read_csv (' data.csv ', nrows =5) 6 print (data) 7

The results of the code run as follows:

Greydemac-mini:chapter06 greyzhang$ python row_test.py  unnamed:0 name_01 coment_01 unnamed:3 unnamed:4 unnamed:5 \ 0   2 name_02 coment_02   nan nan nan   1   3 name_03 coment_03   nan   nan   nan 2   4 name_ Coment_04   nan   nan nan   3   5 name_05 coment_05   nan nan nan   4   6 name_06 coment _06   nan   , nan nan  unnamed:6 0   nan 1   nan 2   nan 3   nan 4   nan Greydemac-mini: Chapter06 greyzhang$

As you can see from the results above, the expected functionality is achieved by specifying the number of rows to read.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.