【发布时间】:2021-01-18 23:29:55
【问题描述】:
数据集: https://github.com/Bene939/newsheadlinedatasets
通过我的程序,我正在标记我的新闻标题数据集。直到今天它工作得很好。 由于某种原因,它不会再写入 csv 文件了。据我所知,数据框已更新。
在我的 csv 的大约 4469 行处,它开始不覆盖 csv 文件。然后它做到了。然后没有再做一次,直到它在第 4474 行完全停止覆盖。直到现在它工作正常,如果我创建一个新的 csv,它将覆盖它。
我正在使用 Jupyter Notebook。这有什么限制吗?带标签的数据集大约 300KB。
!pip install pandas
!pip install pathlib
import pandas as pd
from pathlib import Path
#takes data frame and file name & appends it to given csv
def append_df(df, file_name):
my_file = Path(file_name)
if my_file.exists():
print("Appending to existing file named " + file_name)
orig_df = pd.read_csv(file_name)
print("Old Data Frame: ")
print(orig_df)
new_df = pd.concat([orig_df, df], ignore_index=True).drop_duplicates()
print("New Data Frame: ")
print(new_df)
new_df.to_csv(file_name, index=False, header = True, encoding='utf-8-sig')
else:
print("Creating new file named" + file_name)
news_sentiment_df.to_csv(file_name, index=False, header = True, encoding='utf-8-sig')
#takes data frame and file name & overwrites given csv
def update_csv(df, file_name):
print("Overwriting " + file_name)
df.to_csv(file_name, index=False, header = True, encoding='utf-8-sig')
#shows sentence by sentence, labels it according to input and saves it in a new csv file
print("WARNING: EDITING CSV FILE WITH EXCEL MAY CORRUPT FILE\n")
file_name = "news_headlines.csv"
new_file = "news_headlines_sentiment.csv"
news_sentiment_df = pd.DataFrame(columns=["news", "sentiment"])
my_file = Path(file_name)
if my_file.exists():
df = pd.read_csv(file_name, encoding='utf-8-sig', error_bad_lines=False)
print("Loaded " + file_name)
for index, row in df.iterrows():
user_input = -1
range = [0, 1, 2]
while user_input not in range:
print("####################################################################")
print(row["news"])
try:
user_input = int(input("Negative: 0\nNeutral: 1\nPositive: 2\n"))
except ValueError as err:
print("\nPlease enter an Integer!\n")
pass
new_element = 0
#label sentiment according to input
if user_input == 0:
new_element = [row["news"], 0]
elif user_input == 1:
new_element = [row["news"], 1]
elif user_input == 2:
new_element = [row["news"], 2]
#save labeled sentence to new file
news_sentiment_df.loc[len(news_sentiment_df)] = new_element
append_df(news_sentiment_df, new_file)
#delete data point from original data frame
index_name = df[df["news"] == row["news"]].index
df.drop(index_name, inplace=True)
#update old csv file
update_csv(df, file_name)
else:
print("File not Found")
【问题讨论】:
-
您是否确认问题不在您的 csv 文件中?你能提供数据吗,至少在它开始出错的地方?仅仅 300KB 的数据导致 jupyter notebook 出现故障,这听起来真的不太可能......
-
感谢您的回复。我添加了一个指向标记和未标记数据集的链接。是的,如果 300KB 会导致系统发生故障,那将是非常奇怪的。但据我所知,数据集没有任何问题。也可以用jupyter notebook显示,没有任何问题
-
该程序在我的机器上没有产生任何错误(debian 10 64 位,anaconda python 3.7)。程序读取 4474 行后进入交互模式。这是预期的行为吗?如果不是,您能详细解释一下吗?另外,如果您收到任何错误信息,可以附上吗?
-
是的,该程序也不会对我产生任何错误,但不会按预期运行:它应该将我的输入标签附加到标记数据集的数据框,然后用新数据覆盖 csv框架。但由于某种原因,在接受我的输入后,它没有写入新的 csv。如果您只是在没有标记 csv 的情况下运行它,它将创建一个新的,因此您可以看到它在这种情况下确实有效并且一直有效。此外,当前行将从未标记的数据集中删除。我不知道为什么它突然停止将数据帧写入 csv
-
我尝试制作一个新的 csv 并复制旧内容。同样的结果。所以我猜csv文件也不是问题。问题一定是 append_df 函数。出于某种原因,我认为 concat 不起作用
标签: python pandas dataframe csv