Ahmed Mamdouh
Ahmed Mamdouh

Reputation: 706

How can I make a new Column "Week" into a dataframe in pandas?

My code is as follows:

my_dict = {
    "Date": pd.date_range('2020', freq='D', periods=100),
    "Open": np.random.randn(100),
    "High": np.random.randn(100),
    "Low": np.random.randn(100),
    "Close": np.random.randn(100),
    "Volume": np.random.randn(100),
}

df = pd.DataFrame(my_dict)
display(df)

How can I add "Week" column and values like "2020-01", "2020-02"?

"2020-01" means the first week of 2020.

Upvotes: 0

Views: 405

Answers (4)

Zuhair Abid
Zuhair Abid

Reputation: 189

You can also do the following:

df["Week"] = 1
df["Week"] = pd.to_datetime(df['Date']).dt.to_period('M')

dt.to_period takes value M/Y/D to print month year and date respectively

Upvotes: 0

sudhish
sudhish

Reputation: 98

use datetime. I am using pandas .apply() and a lambda function to get the week formatted.

Since the 'Date' columns is made of timestamp class objects, isocalendar() function returns a tuple ('year','week','day') which is formatted to the way you want.

import datetime
df['Week']=df['Date'].apply(lambda x: "{0}-{1:02d}".format(*list(x.isocalendar())))
df.head(10)

output:

Date    Open    High    Low Close   Volume  Week
0   2020-01-01  -0.628361   -0.019378   0.167120    1.421006    -0.698276   2020-01
1   2020-01-02  -0.515597   0.467128    1.784242    0.358433    0.197478    2020-01
2   2020-01-03  0.781038    0.225310    -0.636053   -0.241801   0.777247    2020-01
3   2020-01-04  1.332335    0.687737    -0.531952   1.554296    -0.243784   2020-01
4   2020-01-05  0.457940    -1.488220   0.408476    -0.196996   -0.970725   2020-01
5   2020-01-06  1.660737    0.610343    -0.769449   -0.854537   -1.203444   2020-02
6   2020-01-07  -0.472873   0.276941    -0.266524   0.450023    1.260696    2020-02
7   2020-01-08  -0.851558   0.092650    0.207837    0.107786    -0.002486   2020-02
8   2020-01-09  0.967156    0.337234    -1.394543   -0.221563   1.231157    2020-02
9   2020-01-10  0.407043    -1.079271   -0.730196   -0.262280   0.367848    2020-02

Upvotes: 0

Mayank Porwal
Mayank Porwal

Reputation: 34046

Do this:

In [2233]: df['Week'] = df.Date.dt.year.astype(str) + '-' + df.Date.dt.week.astype(str).map(lambda x: f'{x:0>2}')

In [2234]: df.Week
Out[2234]: 
0     2020-01
1     2020-01
2     2020-01
3     2020-01
4     2020-01
       ...   
95    2020-14
96    2020-15
97    2020-15
98    2020-15
99    2020-15
Name: Week, Length: 100, dtype: object

Upvotes: 1

ggaurav
ggaurav

Reputation: 1804

Get the year using dt year attribute and concatenate with week attribute. zfill is to fill leading zeros.

(df['Date'].dt.year.astype(str)
     .str.cat(df['Date'].dt.week.astype(str).str.zfill(2),
              sep='-'))

    0     2020-01
    1     2020-01
    2     2020-01
    3     2020-01
    4     2020-01
           ...   
    95    2020-14
    96    2020-15
    97    2020-15
    98    2020-15
    99    2020-15

Upvotes: 2

Related Questions