根据表中另一列中的日期添加句点列

2024-06-16 14:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试根据日期范围向数据框中添加句点列。下面是我的数据帧示例

               story               date  sentiment  price  ccwords  CCWordsCount    fltprice
Story_Num                           
0   it was a curious choice...  2012-01-16  0       $6.68    1.0           1          6.68
1   when he was a yale ...      2013-04-07  0       $162.30  1.0           2          162.30
2   video bitcoin has real...   2013-04-11  0       $124.90  1.0           5          124.90
3   bitcoin s wild ride may...  2013-04-14  0       $90.00    1.0          7          90.00
4   amid the incense cheap...   2013-05-06  1       $112.30  0.0           0          112.30
5   san francisco eight...      2013-05-29  0       $132.30  1.0           1          132.30

因此,我想添加一个列“period”,其中日期在2009年1月至2013年4月之间的任何行都是period 1,2013年5月至2017年12月的任何行都是period 2,2018年1月之后的任何行都是period 3

我已经试过了

df9['period'] = '1' if df9['date'] < '4/30/2013'
df9.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 411 entries, 0 to 410
Data columns (total 7 columns):
story              411 non-null object
date               411 non-null datetime64[ns]
sentiment          411 non-null int64
 bitcoin price     411 non-null object
ccwords            411 non-null float64
CCWordsCount       411 non-null int64
fltprice           411 non-null float64
dtypes: datetime64[ns](1), float64(2), int64(2), object(2)
memory usage: 25.7+ KB

Tags: 数据dateobjectnullpricebitcoinperiodnon
1条回答
网友
1楼 · 发布于 2024-06-16 14:19:59

^{}与日期时间一起使用:

bins = pd.to_datetime(['2000-01-01','2013-04-30','2018-01-31'])
df['new'] = pd.cut(df['date'], bins=bins, labels=[1,2]).cat.add_categories([3]).fillna(3)

^{}^{}

m1 = df['date'].between('2000-01-01','2013-04-30')
m2 = df['date'].between('2013-05-01','2018-01-31')

df['new'] = np.select([m1, m2], [1,2], default=3)

相关问题 更多 >