英文:
Fill NaN values based on condition in pandas
问题
我想要填充'Outline_Level'列中的'N/A'值,使其等于下面单元格的值减1。例如,如果单元格中有一个'N/A'值,而下面的单元格的值为3,我希望'N/A'单元格的值为2。我尝试过bfill方法,它可以填充'N/A'值,但我无法弄清如何从这些值中减去1。
以下是您期望的数据框示例:
Ideal_data = [['1', 'Construction', '', '01/02/2022', '01/06/2022', '1', 'No'],
['2', 'Level Site', 'Construction', '01/02/2022', '01/02/2022', '2', 'No'],
['3', 'Foundation', '', '01/03/2023', '01/06/2023', '1', 'Yes'],
['2', 'Lay Foundation', 'Construction>Foundation', '01/03/2022', '01/04/2022', '3', 'No'],
['1', 'Prepare land for foundation', 'Construction>Foundation', '01/05/2022', '01/06/2022', '3', 'No'],
['2', 'Building Envelope', '', '01/07/2023', '01/16/2023', '1', 'No'],
['2', 'Install Footings', 'Building Envelope', '01/07/2022', '01/07/2022', '2', 'Yes'],
['2', 'Pouring', '', '01/08/202', '01/09/2023', '1', 'No'],
['1', 'Pour Foundation', 'Building Envelope>Pouring', '01/08/2022', '01/09/2022', '3', 'No'],
['2', 'Installation', '', '01/09/2022', '01/14/2022', '1', 'No']]
df2 = pd.DataFrame(Ideal_data, columns=['Outline_level', 'Activity', 'Parent', 'Start', 'Finish', 'WBS Level', 'Match'])
感谢任何帮助。
英文:
I have the below dataframe:
data = [['1', 'Construction', '', '01/02/2022', '01/06/2022', '1', 'No'], ['N/A', 'Level Site', 'Construction', '01/02/2022', '01/02/2022', '2', 'No'], ['3', 'Foundation', '', '01/03/2023', '01/06/2023', '1', 'Yes'],['2', 'Lay Foundation', 'Construction>Foundation', '01/03/2022', '01/04/2022', '3', 'No'], ['N/A', 'Prepare land for foundation', 'Construction>Foundation', '01/05/2022', '01/06/2022', '3', 'No'],['2', 'Building Envelope', '', '01/07/2023', '01/16/2023', '1', 'No'], ['2', 'Install Footings', 'Building Envelope', '01/07/2022', '01/07/2022', '2', 'Yes'], ['2', 'Pouring', '', '01/08/202', '01/09/2023', '1', 'No'],['N/A', 'Pour Foundation', 'Building Envelope>Pouring', '01/08/2022', '01/09/2022', '3', 'No'], ['2', 'Installation', '', '01/09/2022', '01/14/2022', '1', 'No']]
df1 = pd.DataFrame(data, columns=['Outline_level', 'Activity', 'Parent', 'Start', 'Finish', 'WBS Level', 'Match'])
df1
I want to fill the N/A values in the 'Outline_Level' column with the value in the cell below it minus 1. For example, if the cell holds an 'N/A' value, and the cell below has a value of 3, I want the N/A cell to have the value of 2. I have tried the bfill method, which fills the N/A values but I can't figure out how to subtract one from those values.
Here is an example of my ideal dataframe:
Ideal_data = [['1', 'Construction', '', '01/02/2022', '01/06/2022', '1', 'No'], ['2', 'Level Site', 'Construction', '01/02/2022', '01/02/2022', '2', 'No'], ['3', 'Foundation', '', '01/03/2023', '01/06/2023', '1', 'Yes'],['2', 'Lay Foundation', 'Construction>Foundation', '01/03/2022', '01/04/2022', '3', 'No'], ['1', 'Prepare land for foundation', 'Construction>Foundation', '01/05/2022', '01/06/2022', '3', 'No'],['2', 'Building Envelope', '', '01/07/2023', '01/16/2023', '1', 'No'], ['2', 'Install Footings', 'Building Envelope', '01/07/2022', '01/07/2022', '2', 'Yes'], ['2', 'Pouring', '', '01/08/202', '01/09/2023', '1', 'No'],['1', 'Pour Foundation', 'Building Envelope>Pouring', '01/08/2022', '01/09/2022', '3', 'No'], ['2', 'Installation', '', '01/09/2022', '01/14/2022', '1', 'No']]
df2 = pd.DataFrame(Ideal_data, columns=['Outline_level', 'Activity', 'Parent', 'Start', 'Finish', 'WBS Level', 'Match'])
df2
Thank you for any help
答案1
得分: 1
使用 pd.Series.shift
替换识别的索引处的值并移动值:
idx = df1[df1['Outline_level'].eq('N/A')].index
df1.loc[idx, 'Outline_level'] = df1['Outline_level'].shift(-1).iloc[idx].astype(int) - 1
Outline_level Activity Parent ... Finish WBS Level Match
0 1 Construction ... 01/06/2022 1 No
1 2 Level Site Construction ... 01/02/2022 2 No
2 3 Foundation ... 01/06/2023 1 Yes
3 2 Lay Foundation Construction>Foundation ... 01/04/2022 3 No
4 1 Prepare land for foundation Construction>Foundation ... 01/06/2022 3 No
5 2 Building Envelope ... 01/16/2023 1 No
6 2 Install Footings Building Envelope ... 01/07/2022 2 Yes
7 2 Pouring ... 01/09/2023 1 No
8 1 Pour Foundation Building Envelope>Pouring ... 01/09/2022 3 No
9 2 Installation ... 01/14/2022 1 No
[10 rows x 7 columns]
英文:
Replace values on recognized indices and shift values using pd.Series.shift
:
idx = df1[df1['Outline_level'].eq('N/A')].index
df1.loc[idx, 'Outline_level'] = df1['Outline_level'].shift(-1).iloc[idx].astype(int) - 1
Outline_level Activity Parent ... Finish WBS Level Match
0 1 Construction ... 01/06/2022 1 No
1 2 Level Site Construction ... 01/02/2022 2 No
2 3 Foundation ... 01/06/2023 1 Yes
3 2 Lay Foundation Construction>Foundation ... 01/04/2022 3 No
4 1 Prepare land for foundation Construction>Foundation ... 01/06/2022 3 No
5 2 Building Envelope ... 01/16/2023 1 No
6 2 Install Footings Building Envelope ... 01/07/2022 2 Yes
7 2 Pouring ... 01/09/2023 1 No
8 1 Pour Foundation Building Envelope>Pouring ... 01/09/2022 3 No
9 2 Installation ... 01/14/2022 1 No
[10 rows x 7 columns]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论