I have a data frame like this:
Code |
---|
DA |
DB |
DC |
DD |
DE |
DF |
DG |
DX |
DY |
DZ |
And I want to add a column to the table above so that each value in the code column has a fixed value like the table below:
Code | Value |
---|---|
DA | A |
DB | A |
DC | A |
DD | B |
DE | B |
DF | B |
DG | C |
DX | C |
DY | C |
How should I do this ??
CodePudding user response:
df = pd.DataFrame( {'Code': ['DA', 'DB', 'DC', 'DD', 'DE', 'DF', 'DG', 'DX', 'DY']} )
dct = {'DA': 'A', 'DB': 'A', 'DC': 'A',
'DD': 'B', 'DE': 'B', 'DF': 'B',
'DG': 'C', 'DX': 'C', 'DY': 'C'}
df['Value'] = df['Code'].replace(dct)
print(df)
Prints:
Code Value
0 DA A
1 DB A
2 DC A
3 DD B
4 DE B
5 DF B
6 DG C
7 DX C
8 DY C
CodePudding user response:
try this:
Value = ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
df['Value'] = Value