簡體   English   中英

如果在同一列表中找到它們,則根據標題對列進行分組。 Pandas Python

[英]Group columns based on the headers if they are found in the same list. Pandas Python

所以我有一個類似這樣的數據框

Resource    2020-06-01     2020-06-02     2020-06-03
Name1            8               7              8    
Name2            7               9              9 
Name3            10              10             10

想象一下 header 在一個月的所有日子里都是字面意思。 而且名字遠不止三個。

我需要將列減少到五個。 考慮到第一列是2020-06-012020-06-05之間的天數。 然后從同一周的周六到周五。 如果在星期五之前,則為該月的最后一天。 所以六月將是這幾周:

week 1: 2020-06-01 to 2020-06-05
week 2: 2020-06-06 to 2020-06-12
week 3: 2020-06-13 to 2020-06-19
week 4: 2020-06-20 to 2020-06-26
week 5: 2020-06-27 to 2020-06-30

我對這幾周的定義沒有問題。 問題是根據它們對列進行分組。 我什么都想不出來。

有人對此有任何想法嗎?

我必須使用這些代碼來生成您的 dataframe。

dates = pd.date_range(start='2020-06-01', end='2020-06-30')
df = pd.DataFrame({
    'Name1': np.random.randint(1, 10, size=len(dates)),
    'Name2': np.random.randint(1, 10, size=len(dates)),
    'Name3': np.random.randint(1, 10, size=len(dates)),
})
df = df.set_index(dates).transpose().reset_index().rename(columns={'index': 'Resource'})

那么,解決方案就從這里開始。

# Set the first column as index
df = df.set_index(df['Resource'])

# Remove the unused column
df = df.drop(columns=['Resource'])

# Transpose the dataframe
df = df.transpose()

# Output:
Resource    Name1   Name2   Name3
2020-06-01 00:00:00 3   2   7
2020-06-02 00:00:00 5   6   8
2020-06-03 00:00:00 2   3   6
...
# Bring "Resource" from index to column
df = df.reset_index()
df = df.rename(columns={'index': 'Resource'})

# Add a column "week of year"
df['week_no'] = df['Resource'].dt.weekofyear

# You can simply group by the week no column
df.groupby('week_no').sum().reset_index()

# Output:
Resource    week_no Name1   Name2   Name3
0   23  38  42  41
1   24  37  30  43
2   25  38  29  23
3   26  29  40  42
4   27  2   8   3

我不知道你接下來想做什么。 如果您想要原始形式,只需將其transpose()即可。

編輯:OP聲稱這周應該從周六開始,到周五結束

# 0: Monday
# 1: Tuesday
# 2: Wednesday
# 3: Thursday
# 4: Friday
# 5: Saturday
# 6: Sunday
df['weekday'] = df['Resource'].dt.weekday.apply(lambda day: 0 if day <= 4 else 1)
df['customised_weekno'] = df['week_no'] + df['weekday']

Output:

Resource    Resource    Name1   Name2   Name3   week_no weekday customised_weekno
0   2020-06-01  4   7   7   23  0   23
1   2020-06-02  8   6   7   23  0   23
2   2020-06-03  5   9   5   23  0   23
3   2020-06-04  7   6   5   23  0   23
4   2020-06-05  6   3   7   23  0   23
5   2020-06-06  3   7   6   23  1   24
6   2020-06-07  5   4   4   23  1   24
7   2020-06-08  8   1   5   24  0   24
8   2020-06-09  2   7   9   24  0   24
9   2020-06-10  4   2   7   24  0   24
10  2020-06-11  6   4   4   24  0   24
11  2020-06-12  9   5   7   24  0   24
12  2020-06-13  2   4   6   24  1   25
13  2020-06-14  6   7   5   24  1   25
14  2020-06-15  8   7   7   25  0   25
15  2020-06-16  4   3   3   25  0   25
16  2020-06-17  6   4   5   25  0   25
17  2020-06-18  6   8   2   25  0   25
18  2020-06-19  3   1   2   25  0   25

因此,您可以使用customised_weekno進行分組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM