import pandas as pd
file = "test.xlsx"
df = pd.read_excel(file,sheet_name="books")
for id in df['books_id']:
print(id)
Current values
books_id
8
4
5
7
1
Expected result
books_id
1
2
3
4
5
Is there a way to do this by using df.sort_values(by='books_id', key=lambda col: ?????)
or a new method is needed to do this fix sorting?
The logic is as follows: The book ID will always need to start from 1 to N. and I need to fix the previous values so they are correlative from 1,2,3,4,5,6.
if there was 1,3,5. The list should be fixed as 1,2,3.
I want to update the books_id number but not from scratch.
if the ids are 1,3,4 I need to keep 1 and update 3 to 2 and 4 to 3. making it 1,2,3 as final result.
data = {'books_id': [1, 3, 4, 5], 'Title': ['title one', 'title x', 'Title Y', 'Title Z']}
df = pd.DataFrame(data)
RESULT
books_id Title
1 title one
3 titile x
4 Title Y
5 Title Z
EXPECTED RESULT:
books_id Title
1 title one
2 Title Z
3 titile x
4 Title Y