[英]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”、“2020-02”等值?
"2020-01" means the first week of 2020. “2020-01”表示2020年的第一周。
Get the year using dt year attribute and concatenate with week attribute.使用 dt year 属性获取年份并与 week 属性连接。 zfill is to fill leading zeros. zfill是填充前导零。
(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
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
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 dt.to_period取值M/Y/D分别打印年月日
use datetime
.使用datetime
。 I am using pandas .apply()
and a lambda
function to get the week formatted.我正在使用 pandas .apply()
和lambda
function 来格式化星期。
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.由于“日期”列由时间戳 class 对象组成, isocalendar()
function 返回一个元组('year','week','day')
其格式为您想要的方式。
import datetime
df['Week']=df['Date'].apply(lambda x: "{0}-{1:02d}".format(*list(x.isocalendar())))
df.head(10)
output: 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
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”、“2020-02”等值?
"2020-01" means the first week of 2020. “2020-01”是指 2020 年的第一周。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.