【问题标题】:Reading csv file and ignoring the top row [duplicate]读取csv文件并忽略顶行[重复]
【发布时间】:2020-11-03 17:53:16
【问题描述】:

我有一个 xlsx 文件,数据包含如下:

I have to remove the "Magna" key" fom the below excel.

我的代码是:

data = pd.read_excel("abc.xlsx")
data1 = data.replace(np.nan, '', regex=True) #replace NaN with ""
data1.columns = data1.columns.str.replace('Unnamed.*', '') #Removing unNamed
for j in userInputForRemove: #["magna"]
    if j in allElementInExcel: #["contail all elements in xlsx"]
        pattern = re.compile(r'\b'+j+r'\b', flags=re.IGNORECASE)
        data1 = data1.replace(pattern, "", regex=True)
data1.to_excel(i, index=False)

但删除“magna”关键字后,我的输入低于输入。

enter image description here

但我想删除所有“magna”关键字。谁能帮帮我。

【问题讨论】:

  • 你试过read_csvskiprows参数吗?
  • 它不工作@GiladGreen。我试过了
  • 请使用此参数添加尝试
  • 我编辑了这个问题。你能看看这个并给我解决方案吗?
  • 你的标题是“阅读 CSV”,你的代码是 read_excel。 Excel 不是 CSV。您的评论说您“已经尝试过”skiprows 而您的代码没有显示这一点。非常混乱。

标签: python python-3.x pandas list pandas-groupby


【解决方案1】:

您可以使用skiprows:

data = pd.read_csv('path-to-csv.csv', skiprows=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多