在 pandas 中的非唯一(重复)单元格上传播值
propagating values over non-unique (duplicate) cells in pandas
我有以下数据框
import pandas as pd
df=pd.DataFrame({'Players': [ 'Sam', 'Greg', 'Steve', 'Sam',
'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
'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','','Dead','','','','','','','Infected'],
})
print(df)
并且我想将状态值 'infected' 传播给同一地址内的每个人。
这意味着如果不止一个人在同一个地址,并且一个人处于感染状态,那么每个人都会处于这种状态。
所以结果应该是这样的:
df2=pd.DataFrame({'Players': [ 'Sam', 'Greg', 'Steve', 'Sam',
'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
'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','','Dead','Infected','','Infected','','','Infected','Infected'],
})
print(df2)
我该怎么做?到目前为止我试过这个:
df[df.duplicated("Address")]
但它只选择后面的重复项而不是全部
这是一种方法:
In [19]:
infected = df[df['Status']=='Infected'].set_index('Address')
df.loc[df['Address'].isin(infected.index),'Status'] = df['Address'].map(infected['Status']).fillna('')
df
Out[19]:
Address Players Status
0 112 Fake St Sam Infected
1 13 Crest St Greg
2 14 Main St Steve Dead
3 112 Fake St Sam Infected
4 2 Morningwood Jill
5 7 Cotton Dr Bill Infected
6 14 Main St Nod
7 20 Main St Mallory
8 7 Cotton Dr Ping Infected
9 7 Cotton Dr Lamar Infected
因此,这首先构建了状态为 'Infected' 的 df 视图,然后我们将索引设置为地址,这将创建一个查找 table,然后我们可以在其中查找地址map
在 infected
索引和 return 状态。
我在这里使用 loc
仅 select 受感染索引中的地址,其他行保持不变。
我有以下数据框
import pandas as pd
df=pd.DataFrame({'Players': [ 'Sam', 'Greg', 'Steve', 'Sam',
'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
'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','','Dead','','','','','','','Infected'],
})
print(df)
并且我想将状态值 'infected' 传播给同一地址内的每个人。
这意味着如果不止一个人在同一个地址,并且一个人处于感染状态,那么每个人都会处于这种状态。
所以结果应该是这样的:
df2=pd.DataFrame({'Players': [ 'Sam', 'Greg', 'Steve', 'Sam',
'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
'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','','Dead','Infected','','Infected','','','Infected','Infected'],
})
print(df2)
我该怎么做?到目前为止我试过这个:
df[df.duplicated("Address")]
但它只选择后面的重复项而不是全部
这是一种方法:
In [19]:
infected = df[df['Status']=='Infected'].set_index('Address')
df.loc[df['Address'].isin(infected.index),'Status'] = df['Address'].map(infected['Status']).fillna('')
df
Out[19]:
Address Players Status
0 112 Fake St Sam Infected
1 13 Crest St Greg
2 14 Main St Steve Dead
3 112 Fake St Sam Infected
4 2 Morningwood Jill
5 7 Cotton Dr Bill Infected
6 14 Main St Nod
7 20 Main St Mallory
8 7 Cotton Dr Ping Infected
9 7 Cotton Dr Lamar Infected
因此,这首先构建了状态为 'Infected' 的 df 视图,然后我们将索引设置为地址,这将创建一个查找 table,然后我们可以在其中查找地址map
在 infected
索引和 return 状态。
我在这里使用 loc
仅 select 受感染索引中的地址,其他行保持不变。