这是我上一个问题propagating values over non-unique (duplicate) cells in pandas的后续内容
我有一个数据帧:
import pandas as pd
df = pd.DataFrame({'First': ['Sam', 'Greg', 'Steve', 'Sam',
'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
'Last': ['Stevens', 'Hamcunning', 'Strange', 'Stevens',
'Vargas', 'Simon', 'Purple', 'Green', 'Simon', 'Simon'],
'Address': ['112 Fake St',
'13 Crest St',
'14 Main St',
'112 Fake St',
'2 Morningwood',
'7 Cotton Dr',
'14 Main St',
'20 Main St',
'7 Cotton Dr',
'7 Cotton Dr'],
'Status': ['Infected', '', 'Infected', '', '', '', '','', '', 'Infected'],
'Level': [10, 2, 7, 5, 2, 10, 10, 20, 1, 1],
})
假设这一次我想把状态值'infected'传播到同一地址内的每个人,并附加一个条件,比如如果他们在Last中有相同的值。 所以结果看起来像:
df2 = df1.copy(deep=True)
df2['Status'] = ['Infected', '', 'Infected', 'Infected', '', 'Infected', '', '', 'Infected', 'Infected']
如果我想让一个人被标记为感染,如果他在同一个地址,但不是同一级别?结果是:
df3 = df1.copy(deep=True)
df3['Status'] = ['Infected', '', 'Infected', '', '', 'Infected', '', '', '', 'Infected']
我该怎么做?这是一个groupby问题吗?你知道吗
“同一地址”用“groupby”表示。你知道吗
相关问题 更多 >
编程相关推荐