Mathematical modeling using python programming - common excel data is read on demand in rows using python

read raw data

  First import the pandas library
  and then use the read_csv method in the pandas library to read our files. Since the data files and program files are in the same directory, there is no need to use absolute paths

import pandas as pd
data1 = pd.read_csv("data1.csv")

Read the first 20 rows of data

  Here we use the nrows parameter in the read_csv method to get the first few rows of data in the original data

df = pd.read_csv("data1.csv",nrows=20)

  Some people said that I remember that it is better to use the header function. I said that the header parameter is responsible for setting the header row, and the header is a parameter in the read_csv method, which cannot be used to obtain the row data of the specified data. These people It belongs to the kind of memorizing things without remembering and then spraying with your mouth. The head method is a method that can read the first few rows of data. If you don’t use the nrow parameter here, you can also use the head method, as shown below:

import pandas as pd
data1 = pd.read_csv("data1.csv")
data1.head(20)

  Therefore, you must learn things accurately. One of the header and the head is the parameter responsible for the title, and the other is the method of reading the number of lines before the data. You must distinguish them.
  Correspondingly, if you want to read the last few rows of data, it is the tail method

import pandas as pd
data1 = pd.read_csv("data1.csv")
data1.tail(20)

  How many rows before reading data to skip

df1 = pd.read_csv("data1.csv",skiprows = 20)

  There is a problem here, because the first line is the title line, so the 20 lines skipped here include the title line, as shown in the figure: So
insert image description here
  how to skip the line without the title line? We know that python reads from 0, the first line is not called the first line is called the 0th line, that is to say, we need to skip the 0th line and start from the first line of the actual data without the header line Read, the read method is as follows:

df2 = pd.read_csv("data1.csv",skiprows = [i for i in range(1,21)])

  Then some great cleverness came, saying that I don't know how to use this for loop, so what should I do?
  Here is a clever method. For example, if you want to read data other than the first 20 rows of data, you can delete the first 20 rows of data and then read the data normally.

data1.drop(data1.index[0:20],inplace=True) 

insert image description here

  There is also the kind of people who open their mouths after drinking Sanlu milk powder and ask, what does the index mean? You don’t even check this kind of cleverness. If you are like this, I will teach you another method. You first manually delete the first 20 rows of data in excel. How to delete it? First open your hands, turn on the computer, double-click the target excel with the left button, then press the left mouse button to select the first 20 rows of data in excel, as shown in the figure below: after selecting, right-click to delete, and finally
insert image description here
  use the original method ( data1= pd.read_csv("data1.csv")) reads the data and it is ok.
  At this time, there is a need for great cleverness and I don’t know where to get it. Then I said, what should I do if I only need to read even and odd lines?

df3 = pd.read_csv('data1.csv', skiprows=lambda x: (x != 0) and not x % 2)
df4 = pd.read_csv('data1.csv', skiprows=lambda x: x % 2) 

  So far, common excel data is read on-demand in rows using python. Let’s come to an end for now. Next, let’s talk about common excel data read on-demand in columns using python.

Guess you like

Origin blog.csdn.net/weixin_43292788/article/details/131372926