【问题标题】:How to remove a row that has no index number - Python如何删除没有索引号的行 - Python
【发布时间】:2020-08-28 05:36:12
【问题描述】:

我是 Python 新手。

假设我在文本文件中保存了以下非常简单的表格:

one two three
four five six
seven eight nine

我可以使用 pandas 查看此表:

import pandas as pd

df = pd.read_csv('table.txt')
print(df)

这样的结果是:

      one two three
0     four five six
1  seven eight nine

现在,我想删除第一行(即one two three)。我的第一个想法是写:

df.drop([0])

因为行和列是从 0 开始编号的。但是这样做是它删除了第二行(即four five six)。

因此这个问题的标题。如何删除没有索引号的行?因为从print(df)看,第一行没有给索引。

我确实尝试过谷歌这个,但我无法找到答案。这可能是因为我不知道一些关键字。

【问题讨论】:

  • 第一行被解释为标题。使用read_csv("table.txt", header=None) 并删除第一行。
  • 这能回答你的问题吗? How are iloc and loc different?

标签: python pandas row


【解决方案1】:

一二三是CSV文件的头部。 所以跳过它,写下面提到的代码:

df = pd.read_csv('table.txt', header=none)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多