繁体   English   中英

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

[英]How to extract specific portion of CSV file using row key with pandas

我有一个巨大的CSV文件,具有10000行和500列。 我想从标题提取数据到包含device_boot的行。 我想消除device_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,..

因此,我需要使用熊猫在CSV文件中维护最多device_boot行(行)。 我能够删除该关键字上的特定行,但无法使用pd.drop(...)提取到该部分。

感谢您的建议。

采用:

print(df.loc[:df['Name'].gt('device_boot').idxmin()+1,:])

输出将是预期的输出。

更新:

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

如果要删除它,它包含'device_boot'行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM