我有一个名为df
的大型数据框,看起来像:
First Name Last name Dept Location Status Concat
0 Jo Jones Accounts Bristol Current JonesJo
1 Sid Smith Sales Hull New SmithSid
2 Phil Evans Production Hull Current EvansPhil
3 Sarah Heath Marketing Bristol Current HeathSarah
4 Jane Hill Accounts Bristol Current HillJane
5 Amy Cooper Sales Hull Current CooperAmy
23453 Marcus Price Operations Hull Current PriceMarcus
23454 Andrew King Design Bristol Current KingAndrew
23455 Emma Lane Marketing Bristol Current LaneEmma
23456 Brian Deen Accounts Bristol Current DeenBrian
23457 Steve Jacks Design Bristol Current JacksSteve
如果您知道要更改的字段的“坐标”,是否可以更改记录中的字段值。
例如,我试图将Amy Cooper的“部门”值从“销售”更改为“帐户”,我可以这样做吗?
value = 'Accounts'
ConcatName = 'CooperAmy'
columnName = 'Dept'
df.ix[df['Concat']= ConcatName ,columnName ] = value
nb。所有Concat值都是唯一的
所以我得到的数据框看起来像:
First Name Last name Dept Location Status Concat
0 Jo Jones Accounts Bristol Current JonesJo
1 Sid Smith Sales Hull New SmithSid
2 Phil Evans Production Hull Current EvansPhil
3 Sarah Heath Marketing Bristol Current HeathSarah
4 Jane Hill Accounts Bristol Current HillJane
5 Amy Cooper Accounts Hull Current CooperAmy
23453 Marcus Price Operations Hull Current PriceMarcus
23454 Andrew King Design Bristol Current KingAndrew
23455 Emma Lane Marketing Bristol Current LaneEmma
23456 Brian Deen Accounts Bristol Current DeenBrian
23457 Steve Jacks Design Bristol Current JacksSteve
答案 0 :(得分:2)
假设'First Name'
和'Last name'
的组合在数据框中是唯一的,则可以使用MultiIndex
,然后通过pd.DataFrame.at
设置标量:
df = df.set_index(['First Name', 'Last name'])
df.at[('Amy', 'Cooper'), 'Dept'] = 'Accounts'
更改索引后,您将无法再通过df['First Name']
访问系列。为此,请使用df.index.get_level_values('First Name')
。如果在随后的阶段再次需要将索引作为列,则可以使用df = df.reset_index()
。
pd.DataFrame.loc
是更复杂的索引工具,当您具有多个要设置/替换的值时更可取。