Python Pandas如何修复错误标记数据预期字段

时间:2019-08-23 11:30:23

标签: python pandas csv dataframe parsing

我最近开始使用Python,并尝试使用熊猫操作CSV文件。但是,我现在遇到错误:> pandas.errors.ParserError:标记数据时出错。 C错误:第11行中应有3个字段,看到7

我尝试了不同的写入文件的技术,包括用另一个字符串替换而不是将随机数写入文件。

def Create():
    if not isfile('Questions.csv'):
        with open('Questions.csv', 'w') as csvFile:
            Writer = csv.writer(csvFile)
            Writer.writerow(["Question"])
            Writer.writerow(["Ball dropped from", Rand1, "metres. Calculate time taken to reach floor?"])
            Writer.writerow(["Ball dropped from", Rand2, "metres. What is its speed the instant it hits floor?"])
            Writer.writerow(["Ball is projected directly upwards at", Rand3, "meters per second. What is the max height?"])
            Writer.writerow(["Ball is projected directly upwards at", Rand4, "meters per second. How long does it take to reach the highest point?"])
            Writer.writerow(["Initial velocity is", Rand5, ". Accelerating", Rand6, "ms. Calc velocity after", Rand7, "meters"])
            Writer.writerow(["Ball rolled on surface (constant friction) at", Rand8, "meters per second. Stops after", Rand9, "m. Calculate deceleration"])
            Writer.writerow(["Particle accelerating at", Rand10 ,"ms2 from", Rand11,"meters per second to", Rand12,". Calculate distance travelled"])
            Writer.writerow(["Particle moving at", Rand13,"ms. Comes to rest", Rand14,"meters later. After how long did it stop?"])

def get_random_question(df: pd.DataFrame):
    rand_index = random.randint(0, len(df)-1)  # generate random index to get random question
    question = df.loc[rand_index, 'Question']  # get the question for the random index
    print(question)
    return question, rand_index

Create()
Reader = pd.read_csv('Questions.csv')
df = pd.DataFrame(Reader, columns=[1, 2, 3, 4, 5, 6, 7])
Reader1 = pd.read_csv('Questions.csv')
print(Reader1)
get_random_question(Reader1)

(所有“ Rand1,Rand2”等变量都是随机的2个小数位)。 它应该从文件中输出一条随机行,但是相反,我收到许多错误消息;

>Traceback (most recent call last):
  File "C:/Users/Charlie/PycharmProjects/CS-Project/JoshPan.py", line 89, in <module>
    Reader = pd.read_csv('Questions.csv')
  File "C:\Users\Charlie\PycharmProjects\CS-Project\venv\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\Charlie\PycharmProjects\CS-Project\venv\lib\site-packages\pandas\io\parsers.py", line 435, in _read
    data = parser.read(nrows)
  File "C:\Users\Charlie\PycharmProjects\CS-Project\venv\lib\site-packages\pandas\io\parsers.py", line 1139, in read
    ret = self._engine.read(nrows)
  File "C:\Users\Charlie\PycharmProjects\CS-Project\venv\lib\site-packages\pandas\io\parsers.py", line 1995, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 899, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 914, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas\_libs\parsers.pyx", line 968, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 955, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 2172, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 3 fields in line 11, saw 7

0 个答案:

没有答案