Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python标记日期编码并基于季度创建新属性_Python_Python 3.x_Pandas - Fatal编程技术网

使用python标记日期编码并基于季度创建新属性

使用python标记日期编码并基于季度创建新属性,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个日期激活,希望根据日期创建一个新属性。 比如说 Activation date quarter1 quarter2 quarter3 quarter4 2/3/2020(mm/dd/yyyy). 1 0. 0. 0 11/11/2020 0 0 0 1 您可以使用pd.get\u dummie

我有一个日期激活,希望根据日期创建一个新属性。 比如说

Activation date          quarter1    quarter2    quarter3   quarter4
2/3/2020(mm/dd/yyyy).       1            0.           0.        0
11/11/2020                  0            0            0         1

您可以使用
pd.get\u dummies

data = {'Activation_date': {0: '2/3/2020', 1: '11/11/2020', 2 : '01/05/2023' , 3: '09/09/2012', 4:'05/08/2019'}}
df = pd.DataFrame(data)

df = (
    pd.concat([
        df,
        pd.get_dummies(
            pd.to_datetime(df.Activation_date).dt.quarter,
            prefix='quarter')
    ],
    axis=1)
)
输出:

  Activation_date  quarter_1  quarter_2  quarter_3  quarter_4
0        2/3/2020          1          0          0          0
1      11/11/2020          0          0          0          1
2      01/05/2023          1          0          0          0
3      09/09/2012          0          0          1          0
4      05/08/2019          0          1          0          0

你好,你在用熊猫吗?是的,我在用熊猫银行。一个noob问题,但是如何将其附加到现有的数据帧中。使用
pd.concat
@tim83