【问题标题】:Pandas adding blank column title with a count熊猫添加带有计数的空白列标题
【发布时间】:2017-09-09 13:50:21
【问题描述】:

我正在为我的工作处理一些 csv。下面的代码有效,它只有一个小问题。它在“df.insert(0,'ID',' ')”行前面添加一列,列名为空白,并且正在计算行数。发生这种情况有什么原因吗?请随时提供任何见解或挑选我所拥有的东西,因为我是 Python 新手,我相信这可以清理或更有效地运行。

谢谢,

import pandas as pd

# Enter the full file path you want manipulated here
print('Enter Filename Path')
file = input()

# Enter the report category you want added here. This needs to be exact
print('Report Category')
rcat = input()

# Reads the CSV given above
df = pd.read_csv(file)

# This inserts the columns needed to fit the new VulnMan DB
df.insert(0, 'ID', ' ')
df.insert(4, 'Report Category', (rcat))
df.insert(5, 'Status', 'Not Started ')
df.insert(6, 'Comments', ' ')

# Saves the file in the SAME NAME
df.to_csv(file)

【问题讨论】:

标签: python python-3.x pandas


【解决方案1】:

这是DataFrame 的索引。您可以使用

在导出的文件中删除它
df.to_csv(file, index=False)

【讨论】:

  • 这是完美的谢谢。另一个问题。在文件变量中,有没有办法让它总是知道位置,我只提供文件名?例如,这些文件将始终位于“C:\Users\NAME\Downloads”。我可以在上面的代码中添加一些东西让它知道那个位置,然后我只提供“cvsname.csv”吗?
  • 你可以让folder = 'C:\\Users\\NAME\Downloads\\'在某个地方,并在相关的地方使用folder + file
  • 这也很有效。感谢您的帮助,很抱歉长时间延迟联系我们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2017-05-31
  • 2021-09-04
  • 2021-03-28
  • 1970-01-01
  • 2020-04-27
  • 2021-10-29
相关资源
最近更新 更多