如果一列中有特定值,如何导入csv跳过行?

时间:2014-02-14 17:47:28

标签: python csv pandas

Python n00b,这里。我正在使用csv文件中的事件数据。我正在编写一个更改列顺序的脚本,并按时间排序。该部分脚本有效,但我想根据一列的值过滤掉某些行:

Description   Date     Start End   Location         Organization
Meeting       2/14/14  9:00  9:30  Conference Room  Org1
Meeting       2/14/14  9:30  10:00 Conference Room  Org2

如果我不想要Org1,我该如何过滤掉该组会议的行。

我正在使用熊猫:

import pandas as pd
df = pd.read_csv('day_of_the_week.csv')
df = df.sort('MEETING START TIME')
#saved_column = df.column_name #you can also use df['column_name']
location = df.LOCATION
date = df.DATE
starttime = df['MEETING START TIME']
endtime = df['MEETING END TIME']
description = df.DESCRIPTION
organization = df.ORGANIZATION

#write new csv file with new order of columns
df.to_csv('Full_List_sorted.csv', cols=["DATE","MEETING START TIME","MEETING END TIME","DESCRIPTION","ORGANIZATION","LOCATION"],index=False)

由于

2 个答案:

答案 0 :(得分:3)

要从df过滤掉这些行,请执行以下操作:

df = df[df["Organization"]!="Org1"]

另外,如果它有帮助(我本周也开始使用Pandas),这里有一个非常快速和好的教程:

http://manishamde.github.io/blog/2013/03/07/pandas-and-python-top-10/ (那不是我!)

答案 1 :(得分:0)

全部阅读。然后使用pandas搜索创建一个新的数据帧。最后存储新框架