0

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())
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Mayama
  • 49
  • 1
  • 8
  • Please don't post answers inside questions. You are free to [answer your own question](https://stackoverflow.com/help/self-answer) (and even accept it). Anyway, answers should go under the answers section, not inside questions. – Tomerikoo Aug 11 '21 at 15:08
  • Does this answer your question? [Replacing column values in a pandas DataFrame](https://stackoverflow.com/questions/23307301/replacing-column-values-in-a-pandas-dataframe) – Tomerikoo Aug 11 '21 at 15:27

2 Answers2

1

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
C Haworth
  • 659
  • 3
  • 12
1

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)
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41