import pandas as pd from datetime import datetime, timedelta
# 读取 csv df = pd.read_csv('test.csv', low_memory=False) # >>> df # id quote author date # 0 656222 Every moment is a fresh beginning. T.S Eliot 2020-11-11 11:22 # 1 346927 Everything you can imagine is real. Pablo Picasso 2020-11-12 12:23 # 2 995443 Whatever you do, do it well. Walt Disney 2020-11-13 13:24 # 3 281277 When words fail, music speaks. Shakespeare 2020-11-14 14:25 # 4 603115 If God did not exist, it would be necessary to... Voltaire 2020-11-15 15:26 # 5 605452 Brevity is the soul of wit. Shakespeare 2020-11-16 16:27 # >>> df.dtypes # id int64 # quote object # author object # date object # dtype: object
# 过滤 l = datetime.strptime('2020-11-11', '%Y-%m-%d') # 2020-11-11 00:00 r = datetime.strptime('2020-11-12', '%Y-%m-%d') + timedelta(days=1) # 即 2020-11-13 00:00 mask = (df['date'] >= l) & (df['date'] <= r) df.loc[mask] # >>> df.loc[mask] # id quote author date # 0 656222 Every moment is a fresh beginning. T.S Eliot 2020-11-11 11:22:00 # 1 346927 Everything you can imagine is real. Pablo Picasso 2020-11-12 12:23:00
# 排序 df.sort_values(by=['date'], ignore_index=True) # >>> df.sort_values(by=['date'], ignore_index=True) # id quote author date # 0 656222 Every moment is a fresh beginning. T.S Eliot 2020-11-11 11:22:00 # 1 346927 Everything you can imagine is real. Pablo Picasso 2020-11-12 12:23:00 # 2 995443 Whatever you do, do it well. Walt Disney 2020-11-13 13:24:00 # 3 281277 When words fail, music speaks. Shakespeare 2020-11-14 14:25:00 # 4 603115 If God did not exist, it would be necessary to... Voltaire 2020-11-15 15:26:00 # 5 605452 Brevity is the soul of wit. Shakespeare 2020-11-16 16:27:00
# 数据 data = { 'id': [656222, 346927, 995443, 281277, 603115, 605452], 'quote': ['Every moment is a fresh beginning.', 'Everything you can imagine is real.', 'Whatever you do, do it well.', 'When words fail, music speaks.', 'If God did not exist, it would be necessary to invent Him.', 'Brevity is the soul of wit.'], 'author': ['T.S Eliot', 'Pablo Picasso', 'Walt Disney', 'Shakespeare', 'Voltaire', 'Shakespeare'], 'date': ['2020-11-11 11:22' ,'2020-11-12 12:23' ,'2020-11-13 13:24' ,'2020-11-14 14:25' ,'2020-11-15 15:26' ,'2020-11-16 16:27'] }