[英]Dataframe task distribution basing on conditions changing during distribution
[英]How to input data in dataframe basing on multiple conditions
ID | 創建日期 | 上次修改日期 |
---|---|---|
1 | 2021 年 3 月 31 日 8:56 | 2021 年 3 月 31 日 09:46 |
1 | 2021 年 3 月 31 日 5:56 | 2021 年 3 月 31 日 09:48 |
2 | 2021 年 3 月 31 日 0:23 | 2021 年 3 月 31 日 09:47 |
2 | 2021 年 3 月 31 日 6:56 | 2021 年 3 月 31 日 09:46 |
3 | 2021 年 3 月 31 日 7:32 | 2021 年 3 月 31 日 09:46 |
3 | 2021 年 3 月 31 日 8:45 | 2021 年 3 月 31 日 09:46 |
你好,
對於上表,我需要將每個ID的最早創建日期注釋為"Minimal" 。
import pandas as pd
inputFolder = os.getcwd()
filename = filedialog.askopenfilename(title="Select file:", filetypes=(("xlsx files", ".xlsx"), ("all files", "*.*")), initialdir = inputFolder)
df = pd.read_excel(filename, index_col=None, header=0)
df.loc[(df.groupby(['BB Global ID']).agg({'Create Date': min})), 'Comment'] = 'Minimal'
print(df)
我試圖用 pandas df.loc function 來做,但我遇到了以下錯誤。
KeyError: "None of [Index([('C', 'r', 'e', 'a', 't', 'e', ' ', 'D', 'a', 't', 'e')], dtype='object')] are in the [index]"
以下是我想要達到的最終結果:
ID | 創建日期 | 上次修改日期 | 評論 |
---|---|---|---|
1 | 2021 年 3 月 31 日 8:56 | 2021 年 3 月 31 日 09:46 | |
1 | 2021 年 3 月 31 日 5:56 | 2021 年 3 月 31 日 09:48 | 最小 |
2 | 2021 年 3 月 31 日 0:23 | 2021 年 3 月 31 日 09:47 | 最小 |
2 | 2021 年 3 月 31 日 6:56 | 2021 年 3 月 31 日 09:46 | |
3 | 2021 年 3 月 31 日 7:32 | 2021 年 3 月 31 日 09:46 | 最小 |
3 | 2021 年 3 月 31 日 8:45 | 2021 年 3 月 31 日 09:46 |
使用GroupBy.transform
重復聚合值,因此可以按原始列進行比較:
mask = df.groupby(['BB Global ID'])['Create Date'].transform(min).eq(df['Create Date'])
df.loc[mask, 'Comment'] = 'Minimal'
或者:
df['Comment'] = np.where(mask, 'Minimal', '')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.