根据一列对DateFrame进行筛选的三种方法


# encoding=utf-8

import pandas as pd
from pandasql import sqldf

ls = [ { 'id' : 1, 'time': 1, },{ 'id' : 2,'time': 3,}, {'id' : 3,'time': 3, }]

df = pd.DataFrame(ls)
print(df)

# 第一种:简单粗暴
print(df[df['time'] > 1])

# 第二种: pandassql的sqldf方法
pysqldf = lambda sql: sqldf(sql, globals())
sql = 'select * from df where time > 1'
print(pysqldf(sql))

# 第三种: where()  这种可读性更好些
df1 = df.where(cond=df['time'] > 1)
print(df1.dropna())


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM