Reputation: 59
How can i change the rows in my csv data? I have a csv data which contains a column class
and the coordinates.
class x1 y1 ... x45 y45
A 0.187087 0.668525 ... -0.024700 0.220235
B 0.202503 0.669253 ... -0.107100 0.240229
....
C 0.248009 0.676325 ... -0.070317 0.278087
C 0.245750 0.658381 ... -0.077429 0.282217
D 0.235889 0.643202 ... -0.080697 0.262705
I would like to change the String from my column class
in numbers like this:
class x1 y1 ... x45 y45
0 0.187087 0.668525 ... -0.024700 0.220235
1 0.202503 0.669253 ... -0.107100 0.240229
....
2 0.248009 0.676325 ... -0.070317 0.278087
2 0.245750 0.658381 ... -0.077429 0.282217
3 0.235889 0.643202 ... -0.080697 0.262705
How can i do this? Only the column 'class' is to be changed. Everything else should remain as it is. Ive tried something but its just chnages the columns headers.
df = pd.read_csv('data.csv')
print(df.head())
df.rename({'A':'0', 'B':'1', 'C':'2', 'D':'3', 'E':'4', 'F':'5', 'O':'6',}, axis=1, inplace=True)
print(df.head())
Upvotes: 0
Views: 337
Reputation: 24314
If I Understand Correctly:
you can try getting the group number of 'class' column:
df['class']=df.groupby('class',sort=False).ngroup()
OR
If you want custom values then you can either map or replace those values:
d={'A':'0', 'B':'1', 'C':'2', 'D':'3', 'E':'4', 'F':'5', 'O':'6'}
df['class']=df['class'].map(d)
Upvotes: 1
Reputation: 659
If you have a large number of class values and don't want to write the dictionary by hand you can convert to a categorical variable and then take an encoding.
df['class'] = df['class'].astype('category').cat.codes
Upvotes: 1