跳过csv文件中所需数据上方和下方的行

时间:2019-02-21 22:13:15

标签: python pandas csv numpy

我有多个csv文件,看起来像这样:

>>> print(df)
    x x.1 x.2 x.3  ... Unnamed: 33 Unnamed: 34 Unnamed: 35 Unnamed: 36
0   x   x   x   x  ...           x           x           x           x
1   x   x   x   x  ...           x           x           x           x
2   x   x   x   x  ...         NaN         NaN         NaN         NaN
3   x   x   x   x  ...         NaN         NaN         NaN         NaN
4   x   x   x   x  ...         NaN         NaN         NaN         NaN
5   x   x   x   x  ...         NaN         NaN         NaN         NaN
6   x   x   x   x  ...         NaN         NaN         NaN         NaN
7   x   x   x   x  ...         NaN         NaN         NaN         NaN
8   x   x   x   x  ...         NaN         NaN         NaN         NaN
9   x   x   x   x  ...         NaN         NaN         NaN         NaN
10  x   x   x   x  ...         NaN         NaN         NaN         NaN
11  x   x   x   x  ...         NaN         NaN         NaN         NaN
12  x   x   x   x  ...         NaN         NaN         NaN         NaN
13  x   x   x   x  ...         NaN         NaN         NaN         NaN
14  A   A   A   A  ...         NaN         NaN         NaN         NaN
15  B   B   B   B  ...         NaN         NaN         NaN         NaN
16  C   C   C   C  ...         NaN         NaN         NaN         NaN
17  D   D   D   D  ...         NaN         NaN         NaN         NaN
18  E   E   E   E  ...         NaN         NaN         NaN         NaN
19  F   F   F   F  ...         NaN         NaN         NaN         NaN
20  x   x   x   x  ...         NaN         NaN         NaN         NaN
21  x   x   x   x  ...         NaN         NaN         NaN         NaN
22  x   x   x   x  ...         NaN         NaN         NaN         NaN
23  x   x   x   x  ...         NaN         NaN         NaN         NaN
24  x   x   x   x  ...         NaN         NaN         NaN         NaN

[25 rows x 37 columns]

此csv文件中有很多不同类型的数据,但是我需要的唯一数据是标记为A-F的数据。我有大量的这些csv文件,所以我想做的就是将它们合并在一起,但只合并我想要的数据。

我有两种方法,一种优于另一种。

(1)我非常想要的数据总是出现在第14-19行上,并且有4列长。因此,我每次阅读这些csv文件之一时都在想什么,我可以跳过14上方和19下方的行,但是我不确定该怎么做?

类似data = pd.read_csv(file,skiprows=[0:14])的东西,但是我也想跳过19岁以后的任何行吗?有没有办法只用列14-19加载行0-4

(2)我的第二个想法是,我不确定是否可以,但是如果数据没有出现在一个文件的第14-19行中,也许我可以让python进行某种搜索对于我想要的数据,它将消除走错行的任何错误?

感谢您的帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

pandas还有一个附加参数nrows,可用于仅读取指定数量的行

>>> import pandas as pd
>>> df = pd.read_csv(filename, skiprows=list(range(14)), n_rows=6)
>>> df
    x x.1 x.2 x.3  ... Unnamed: 33 Unnamed: 34 Unnamed: 35 Unnamed: 36
0   A   A   A   A  ...         NaN         NaN         NaN         NaN
1   B   B   B   B  ...         NaN         NaN         NaN         NaN
2   C   C   C   C  ...         NaN         NaN         NaN         NaN
3   D   D   D   D  ...         NaN         NaN         NaN         NaN
4   E   E   E   E  ...         NaN         NaN         NaN         NaN
5   F   F   F   F  ...         NaN         NaN         NaN         NaN

答案 1 :(得分:1)

遵循第二个想法“万一数据没有出现在一个文件的第14-19行中”:

 #getting the desired rows
df_desired = data.loc[  (data['x'] == 'A') | (data['x'] == 'B')|(data['x'] == 'C') | (data['x'] == 'E')| (data['x'] == 'F')]

 #getting the first 4 columns
df=df.ix[:,[0:4]]