如何按多个条件拆分 pandas 数据框
How to split pandas data frame by many criteria
我有大约 150,000 行数据,按域、电子邮件模板、退回类型和每天的退回次数详细说明电子邮件退回的情况。它的格式如下:
+--------+-------------+-----------------+-------+---------+-------+
| t | bounce_type | source_ip | tid | emld | count |
+--------+-------------+-----------------+-------+---------+-------+
| 1/1/15 | hard | 199.122.255.142 | 10033 | aol.com | 4 |
+--------+-------------+-----------------+-------+---------+-------+
从所有源 ips 和所有 tids 到 select 只有 emld 为 "aol.com"、弹跳类型为 "hard" 的行的最简单方法是什么?这是我要为其创建函数并传递数据框的东西,还是有更简单的操作来按这些条件过滤数据?
一个简单的方法是执行屏蔽,假设你的 DataFrame 名为 df
,它将是这样的:
masked = (df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')
# then the result will be
df[masked]
一行简写版本:
df[(df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')]
仅 return source_ip
和 tids
列:
df[masked][['source_ip', 'tids']]
或者,
df[(df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')][['source_ip', 'tids']]
希望对您有所帮助。
我有大约 150,000 行数据,按域、电子邮件模板、退回类型和每天的退回次数详细说明电子邮件退回的情况。它的格式如下:
+--------+-------------+-----------------+-------+---------+-------+
| t | bounce_type | source_ip | tid | emld | count |
+--------+-------------+-----------------+-------+---------+-------+
| 1/1/15 | hard | 199.122.255.142 | 10033 | aol.com | 4 |
+--------+-------------+-----------------+-------+---------+-------+
从所有源 ips 和所有 tids 到 select 只有 emld 为 "aol.com"、弹跳类型为 "hard" 的行的最简单方法是什么?这是我要为其创建函数并传递数据框的东西,还是有更简单的操作来按这些条件过滤数据?
一个简单的方法是执行屏蔽,假设你的 DataFrame 名为 df
,它将是这样的:
masked = (df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')
# then the result will be
df[masked]
一行简写版本:
df[(df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')]
仅 return source_ip
和 tids
列:
df[masked][['source_ip', 'tids']]
或者,
df[(df['emld'] == 'aol.com') & (df['bounce_type'] == 'hard')][['source_ip', 'tids']]
希望对您有所帮助。