Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用带熊猫的行键提取CSV文件的特定部分_Python_Pandas_Csv_Dataframe_Extract - Fatal编程技术网

Python 如何使用带熊猫的行键提取CSV文件的特定部分

Python 如何使用带熊猫的行键提取CSV文件的特定部分,python,pandas,csv,dataframe,extract,Python,Pandas,Csv,Dataframe,Extract,我有一个巨大的CSV文件,有10000行和500列。我想将数据从标题提取到包含device\u boot的行。我想消除设备启动后的所有行 例如: Name,Time,status,.. start,05:06:2018 10:10:23,good,.. start,05:06:2018 10:11:23,good,.. failure,05:06:2018 11:10:25,critical,.. device_boot,05:06:2018 13:11:25,reboot,.. start,0

我有一个巨大的CSV文件,有10000行和500列。我想将数据从标题提取到包含
device\u boot
的行。我想消除
设备启动后的所有行

例如:

Name,Time,status,..
start,05:06:2018 10:10:23,good,..
start,05:06:2018 10:11:23,good,..
failure,05:06:2018 11:10:25,critical,..
device_boot,05:06:2018 13:11:25,reboot,..
start,05:06:2018 13:13:23,good,..
start,05:06:2018 13:16:23,good,..
因此,我需要使用pandas在CSV文件中维护最多
device\u boot
line(行)。我可以删除该关键字上的特定行,但无法使用
pd.drop(…)
提取到该部分

感谢您的建议。

使用:

print(df.loc[:df['Name'].gt('device_boot').idxmin()+1,:])
输出将是预期输出

更新:

print(df.loc[:df.index[df['Name']=='device_boot'].tolist()[-1],:])
其中包含
“设备启动”
行,如果要删除它:

print(df.loc[:df.index[df['Name']=='device_boot'].tolist()[-1]-1,:])

我找不到关键字的索引,比如

val = df.loc[df['name']=='device_boot'].index
print val
然后,使用该行索引并仅检索该变量

rowretrive_index = val1+50  // any extra rows can be added here.
print rowretrive_index

df1 = df.iloc[1:rowretrive_index]
df1.to_csv('/out.csv',',',dtype='unicode8')
希望它会有用。 谢谢
Sundar

欢迎来到StackOverflow。请花点时间阅读这篇文章,以及如何提供答案,并相应地修改你的问题。这些提示可能也很有用。你为什么不记录索引号呢?我想这会更容易,你可以使用
nrows
。类似于:
data=pd.read\u csv(“filename.csv”,nrows=1312)
。只要找到行数,我就会使用简单的bash命令,比如`wc``或简单的for循环..是的,我知道行数并将行提取到关键字,但有时由于文件太大而无法知道行数。对于基于索引获取行,我使用df=pd.read_csv(/home/ubuntu1/output.csv',low_memory=False,index_col=False,dtype='unicode8',nrows=590)是的,您的答案只对特定行进行过滤,但我的期望是“如何将行提取到第一行的设备启动字”。在一些csv文件中,设备启动也会出现多次,我需要提取csv文件中的所有行,直到最后一次设备启动出现在第一列。