Home > Back-end >  How to remove row numbering in pandas
How to remove row numbering in pandas

Time:01-18

I read first 3 rows of csv file from link using pandas library with below code:

import pandas as pd
data = pd.read_csv(url, nrows=3)
print(data)

it returns: book_dataset_image

But I don't want the row numbering like 0 1 2

How should I remove them?

CodePudding user response:

You can't. Pandas need an index to your rows and columns. Maybe you can define book_id column as index row:

data = pd.read_csv(url, index_col='book_id', nrows=3)

Update

Basically, I want to convert data to json so if I shouldn't remove row numbering, data.to_json() returns {"book_id":{"0":1},"goodreads_book_id":{"0":2767052} ... . But I want it like this: { "book_id":1, "goodreads_book_id":2767052, ...... Is there other way to do it?

data = pd.read_csv(url, nrows=3)
out = df.to_json(orient='records', indent=4)
print(out)

# Output
[
    {
        "book_id":15,
        "goodreads_book_id":111
    },
    {
        "book_id":16,
        "goodreads_book_id":112
    }
]
  •  Tags:  
  • Related