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

标签 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,我该如何过滤掉该组 session 的行。

我正在使用 Pandas :

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)

谢谢

最佳答案

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

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

此外,如果它有帮助(我这周才开始使用 Pandas),这里有一个非常快速和不错的教程:

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

关于python - 如果一列中有特定值,如何导入 csv 跳过行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786124/

相关文章:

python - Python中列表列表中的列表中的值的平均值

python - 删除大文件中的空列

python - 仅当列没有值时,Pandas DataFrame 才从另一个数据帧更新

ios - 将 NSArray 写入计算机上的文件一次

unix - 如何删除包含用双引号括起来的字符串的单元格值的 CSV 文件的一列/多列

Python、Pandas、倒置展开窗口

python - "Index Match"Excel 中的功能未在 pandas Merge 中捕获(或者是?)

python - 在 python lxml 中设置和访问命名空间

python - 检查包含超过 64 个字符的数据框列

python - 如何使用 SQLAlchemy 在 Postgres 数据库中创建包含 UUID 列的表?