过滤 Pandas 中列名包含模式的多列的数据帧
Filtering DataFrames in Pandas for multiple columns where a column name contains a pattern
在过滤多列时,我看到了一些示例,我们可以使用类似这样的方法过滤行 df[df['A'].str.contains("string") | df['B'].str.contains("string")]
。
我有多个文件,我想在其中获取每个文件,并从其中包含 'email'
字符串的列名中仅获取带有 'gmail.com'
的那些行。
所以一个例子 header 可以是这样的:'firstname' 'lastname' 'companyname' 'address' 'emailid1' 'emailid2' 'emailid3' ...
列 emailid1..2..3
的电子邮件 ID 包含 gmail.com
。我想获取 gmail 可以出现在其中任何一行的行。
for file in files:
pdf = pd.read_csv('Reduced/'+file,delimiter = '\t')
emailids = [col for col in pdf.columns if 'email' in col]
# pdf['gmail' in pdf[emailids]]
您可以使用 any
with boolean indexing
:
pdf = pd.DataFrame({'A':[1,2,3],
'email1':['gmail.com','t','f'],
'email2':['u','gmail.com','t'],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})
print (pdf)
A D E F email1 email2
0 1 1 5 7 gmail.com u
1 2 3 3 4 t gmail.com
2 3 5 6 3 f t
#filter column names
emailids = [col for col in pdf.columns if 'email' in col]
print (emailids)
['email1', 'email2']
#apply string function for each filtered column
df = pd.concat([pdf[col].str.contains('gmail.com') for col in pdf[emailids]], axis=1)
print (df)
email1 email2
0 True False
1 False True
2 False False
#filter at least one True by any
print (pdf[df.any(1)])
A D E F email1 email2
0 1 1 5 7 gmail.com u
1 2 3 3 4 t gmail.com
给出的示例输入:
df = pd.DataFrame({'email': ['test@example.com', 'someone@gmail.com'], 'somethingelse': [1, 2], 'another_email': ['whatever@example.com', 'something@example.com']})
例如:
another_email email somethingelse
0 whatever@example.com test@example.com 1
1 something@example.com someone@gmail.com 2
您可以过滤掉包含电子邮件的列,查找 gmail.com
或您想要的任何文本,然后子集,例如:
df[df.filter(like='email').applymap(lambda L: 'gmail.com' in L).any(axis=1)]
这给你:
another_email email somethingelse
1 something@example.com someone@gmail.com 2
在过滤多列时,我看到了一些示例,我们可以使用类似这样的方法过滤行 df[df['A'].str.contains("string") | df['B'].str.contains("string")]
。
我有多个文件,我想在其中获取每个文件,并从其中包含 'email'
字符串的列名中仅获取带有 'gmail.com'
的那些行。
所以一个例子 header 可以是这样的:'firstname' 'lastname' 'companyname' 'address' 'emailid1' 'emailid2' 'emailid3' ...
列 emailid1..2..3
的电子邮件 ID 包含 gmail.com
。我想获取 gmail 可以出现在其中任何一行的行。
for file in files:
pdf = pd.read_csv('Reduced/'+file,delimiter = '\t')
emailids = [col for col in pdf.columns if 'email' in col]
# pdf['gmail' in pdf[emailids]]
您可以使用 any
with boolean indexing
:
pdf = pd.DataFrame({'A':[1,2,3],
'email1':['gmail.com','t','f'],
'email2':['u','gmail.com','t'],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})
print (pdf)
A D E F email1 email2
0 1 1 5 7 gmail.com u
1 2 3 3 4 t gmail.com
2 3 5 6 3 f t
#filter column names
emailids = [col for col in pdf.columns if 'email' in col]
print (emailids)
['email1', 'email2']
#apply string function for each filtered column
df = pd.concat([pdf[col].str.contains('gmail.com') for col in pdf[emailids]], axis=1)
print (df)
email1 email2
0 True False
1 False True
2 False False
#filter at least one True by any
print (pdf[df.any(1)])
A D E F email1 email2
0 1 1 5 7 gmail.com u
1 2 3 3 4 t gmail.com
给出的示例输入:
df = pd.DataFrame({'email': ['test@example.com', 'someone@gmail.com'], 'somethingelse': [1, 2], 'another_email': ['whatever@example.com', 'something@example.com']})
例如:
another_email email somethingelse
0 whatever@example.com test@example.com 1
1 something@example.com someone@gmail.com 2
您可以过滤掉包含电子邮件的列,查找 gmail.com
或您想要的任何文本,然后子集,例如:
df[df.filter(like='email').applymap(lambda L: 'gmail.com' in L).any(axis=1)]
这给你:
another_email email somethingelse
1 something@example.com someone@gmail.com 2