我只想在“时间轴”列上过滤“ Jan ”。
Timeline Goods Price
Jan-2018 Sugar 100
Jan-2017 Wheat 150
Feb- 2018 Sugar 120
Feb-2017 Sugar 125
输出:
Timeline Goods Price
Jan-2018 Sugar 100
Jan-2017 Wheat 150
答案 0 :(得分:1)
将str.startswith
与boolean indexing
一起使用:
df = df[df['Timeline'].str.startswith('Jan')]
print (df)
Timeline Goods Price
0 Jan-2018 Sugar 100
1 Jan-2017 Wheat 150
如果使用datetime
,则比较dt.month
:
df['Timeline'] = pd.to_datetime(df['Timeline'])
df = df[df['Timeline'].dt.month==1]
print (df)
Timeline Goods Price
0 2018-01-01 Sugar 100
1 2017-01-01 Wheat 150