Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫不更新CSV_Python_Pandas_Dataframe_Csv - Fatal编程技术网

Python 熊猫不更新CSV

Python 熊猫不更新CSV,python,pandas,dataframe,csv,Python,Pandas,Dataframe,Csv,数据集: 通过我的程序,我标记了我的新闻标题数据集。到今天为止,一切都很顺利。 由于某种原因,它将不再写入csv文件。据我所知,数据帧得到了更新 在我的csv文件大约4469行时,它开始不覆盖csv文件。然后它做到了。然后,直到它在第4474行完全停止覆盖,才再次执行该操作。它工作良好,直到现在,如果我创建一个新的csv它将覆盖它 我用的是Jupyter笔记本。这有什么限制吗?标记的数据集大约为300KB !pip install pandas !pip install pathlib i

数据集:

通过我的程序,我标记了我的新闻标题数据集。到今天为止,一切都很顺利。 由于某种原因,它将不再写入csv文件。据我所知,数据帧得到了更新

在我的csv文件大约4469行时,它开始不覆盖csv文件。然后它做到了。然后,直到它在第4474行完全停止覆盖,才再次执行该操作。它工作良好,直到现在,如果我创建一个新的csv它将覆盖它

我用的是Jupyter笔记本。这有什么限制吗?标记的数据集大约为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")

我试图在使用drop_duplicates函数时添加重复项,但没有注意到它

我试图在使用drop_duplicates函数时添加重复项,但没有注意到它

是否已验证问题不在您的csv文件中?请您提供数据,至少在哪里开始出错?仅300KB的数据就导致jupyter笔记本出现故障,这听起来真的不太可能……谢谢您的回复。我添加了一个指向已标记和未标记数据集的链接。是的,如果300KB会导致系统故障,那将非常奇怪。但据我所知,数据集没有任何问题。它可以在jupyter笔记本电脑上显示,没有任何问题。该程序在我的机器上没有产生任何错误(debian 10 64位,anaconda python 3.7)。程序在读取4474行后进入交互模式。这是预期的行为吗?如果没有,请详细解释?此外,如果您收到任何错误消息,请附加它好吗?是的,该程序也不会为我产生任何错误,但行为不符合预期:它应该将我的输入标签附加到标记数据集的数据框中,然后用新的数据框覆盖csv。但由于某种原因,在接受我的输入后,它不会写入新的csv。如果您只是在没有标记csv的情况下运行它,它将创建一个新的csv,这样您就可以看到它在这种情况下确实有效,并且一直有效到现在。此外,当前行将从未标记的数据集中删除。我不知道为什么它突然停止向csvi写入数据帧,尝试创建新的csv并复制旧内容。同样的结果。所以我猜csv文件也不是问题所在。问题一定是append_df函数。由于某些原因,concat不起作用,我想您是否已验证问题不在您的csv文件中?请您提供数据,至少在哪里开始出错?仅300KB的数据就导致jupyter笔记本出现故障,这听起来真的不太可能……谢谢您的回复。我添加了一个指向已标记和未标记数据集的链接。是的,如果300KB会导致系统故障,那将非常奇怪。但据我所知,数据集没有任何问题。它可以在jupyter笔记本电脑上显示,没有任何问题。该程序在我的机器上没有产生任何错误(debian 10 64位,anaconda python 3.7)。程序在读取4474行后进入交互模式。这是预期的行为吗?如果没有,请详细解释?此外,如果您收到任何错误消息,请附加它好吗?是的,该程序也不会为我产生任何错误,但行为不符合预期:它应该将我的输入标签附加到标记数据集的数据框中,然后用新的数据框覆盖csv。但由于某种原因,在接受我的输入后,它不会写入新的csv。如果您只是在没有标记csv的情况下运行它,它将创建一个新的csv,这样您就可以看到它在这种情况下确实有效,并且一直有效到现在。此外,当前行将从未标记的数据集中删除。我不知道为什么它突然停止向csvi写入数据帧,尝试创建新的csv并复制旧内容。同样的结果。所以我猜csv文件也不是问题所在。问题一定是append_df函数。我想由于某种原因,康考特不起作用