简体   繁体   English

如何在 pandas 中将一个新的“周”列变成 dataframe?

[英]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.

相关问题 如何使用多索引将新列分配给 pandas DataFrame 的切片? - How can I assign a new column to a slice of a pandas DataFrame with a multiindex? 如何在Pandas数据框中的新列中添加Python对象? - How can I add a Python Object to new column in a pandas dataframe? 如何使 pandas dataframe 列标题全部小写? - How can I make pandas dataframe column headers all lowercase? Pandas dataframe,如何按单列分组并将总和应用于多列并添加新的总和列? - Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column? Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 在 pandas dataframe 中,如何根据列值过滤行,进行计算并将结果分配给新列? - In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column? 如何通过列中的名称在 PANDAS 中创建新的数据框 - How to make new dataframe within PANDAS by names within a column 熊猫使用旧的列名称创建新的数据框 - Pandas make a new dataframe with the old column names 如果列的 len == 1,如何在周列 dataframe pandas 前面添加 0 - How to add 0 infront of week column dataframe pandas if len of column == 1 Pandas:如何在 pandas Z6A8064B5DF4794505500553C47D5 的列上使用 map 来创建新的列? 使用 lambda function 执行此操作时遇到问题 - Pandas: how can I use map on a column in a pandas dataframe to create a new column? Having trouble using a lambda function to do this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM