社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Anurag Dabas

Anurag Dabas 最近创建的主题
Anurag Dabas 最近回复了

试试via split() , join() 和 to_datetime() 方法:

df['timestamp']=pd.to_datetime(df['timestamp'].str.split(':',1).str.join(' '),errors='coerce')

产量 df :

    timestamp               col1
0   2021-05-10 19:48:36     714
1   2021-05-10 00:28:12     None
2   2021-05-10 15:34:26     634
3   2021-05-10 00:20:25     8453
4   2021-05-10 14:31:32     None

你可以试试via str.title() , str.contains() 和 astype() 方法:

df['col2']=df['col1'].str.title().str.contains('|'.join(bad_list)).astype(int)

产量 df :

    col1            col2
0   John Simon prd  0
1   agc Ann White   0
2   BeN and Ann     1  

代码的逐步分解:

由于您的列表(即bad_列表)包含格式中的单词(Ist word为大写,其余都很小),所以我们使用 Series.str.title() 所以现在这个系列(‘col1’)看起来像:

0    John Simon Prd
1     Agc Ann White
2       Ben And Ann
Name: col1, dtype: object

然后我们使用 str.contains() 在检查bad_列表中的任何元素是否存在于序列行('col1')中后,我们得到了一个布尔序列:

0    False
1    False
2     True
Name: col1, dtype: bool

注:

这是里面的代码 contains() 方法:

'|'.join(bad_list)
#giving you a string(output of above code):
'Ben|Wayne'

最后,我们将布尔级数通过 astype() 方法:

0    0
1    0
2    1
Name: col1, dtype: int32

或

另一种方法是使用 IGNORECASE 来自 re @seanbean在评论中建议的模块:

from re import IGNORECASE

df['col2']=df['col1'].str.contains('|'.join(bad_list), flags=IGNORECASE).astype(int)
5 年前
回复了 Anurag Dabas 创建的主题 » python数据帧过滤or和and的组合

你可以试试看 loc + isin() + eq() :

filtered_df =df.loc[(df['colA'].isin(['alpha','beta'])) & (df['colB'].eq('gamma'))]

现在如果你打印 filtered_df 您将获得过滤后的数据帧