[英]How to remove row numbering in pandas
I read first 3 rows of csv file from link using pandas library with below code:我使用带有以下代码的 pandas 库从链接中读取了 csv 文件的前 3 行:
import pandas as pd
data = pd.read_csv(url, nrows=3)
print(data)
But I don't want the row numbering like 0 1 2
但我不希望行编号像
0 1 2
How should I remove them?我应该如何删除它们?
You can't.你不能。 Pandas need an index to your rows and columns.
Pandas 需要您的行和列的索引。 Maybe you can define
book_id
column as index row:也许您可以将
book_id
列定义为索引行:
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}... .
基本上,我想将数据转换为 json,所以如果我不应该删除行编号,data.to_json() 将返回 {"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?
但我想要这样: { "book_id":1, "goodreads_book_id":2767052, ...... 还有其他方法吗?
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
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.