'Indexing not working properly in DataFrame
df
| Unnamed: 0 | Name | Age | Gender | Height | |
|---|---|---|---|---|---|
| 0 | 0 | Asish | 20 | m | 5.11 |
| 1 | 1 | Meghali | 23 | f | 5.9 |
| 2 | 2 | Parimita | 49 | f | 5.6 |
| 3 | 3 | SatyaNarayan | 60 | m | 5.1 |
df.reset_index(drop=True,inplace=True)
df
| Unnamed: 0 | Name | Age | Gender | Height | |
|---|---|---|---|---|---|
| 0 | 0 | Asish | 20 | m | 5.11 |
| 1 | 1 | Meghali | 23 | f | 5.9 |
| 2 | 2 | Parimita | 49 | f | 5.6 |
| 3 | 3 | SatyaNarayan | 60 | m | 5.1 |
df=df.reset_index(drop=True)
df
| Unnamed: 0 | Name | Age | Gender | Height | |
|---|---|---|---|---|---|
| 0 | 0 | Asish | 20 | m | 5.11 |
| 1 | 1 | Meghali | 23 | f | 5.9 |
| 2 | 2 | Parimita | 49 | f | 5.6 |
| 3 | 3 | SatyaNarayan | 60 | m | 5.1 |
I have tried these above mentioned steps. However, they doesn't seem to resolve. I want something like below:
| Name | Age | Gender | Height | |
|---|---|---|---|---|
| 0 | Asish | 20 | m | 5.11 |
| 1 | Meghali | 23 | f | 5.9 |
| 2 | Parimita | 49 | f | 5.6 |
| 3 | SatyaNarayan | 60 | m | 5.1 |
Solution 1:[1]
Unnamed: 0 is not an index column. If you want to drop that:
df.drop('Unnamed: 0', axis=1)
Solution 2:[2]
If you're reading from CSV as hinted in your other question, the best might be to tell read_csv that this first column is in fact the index:
df = pd.read_csv('your_file.csv', index_col=0)
And if you really have no use for the index in the CSV, do not have it in the first place.
When you initially save the file, do:
df.to_csv('your_file.csv', index=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Drach Noi |
| Solution 2 | mozway |
