简体   繁体   English

用值 Python-Pandas 替换空单元格

[英]Replacing empty cells with values Python-Pandas

I have a csv file that looks like this我有一个看起来像这样的 csv 文件

col1,col2,col3,col4,col5

value1,value2,value3,value4,value5

,,value6,value7,value8

,,value10,value11,value12

I would need to insert values in the empty cells.我需要在空单元格中插入值。

I am reading the data with pandas like this我正在像这样使用 pandas 读取数据

import pandas as pd

data = pd.read_csv(file).fillna('yellow', 'blue') 

any sugestions?有什么建议吗?

Update : the error is solved The rows in my csv where an editing mistake.更新:错误已解决 我的 csv 中的行编辑错误。 The main problem is how to write in to the empty cells custom values.主要问题是如何将自定义值写入空单元格。

Expected result预期结果

col1,col2,col3,col4,col5

value1,value2,value3,value4,value5

yellow,blue,value6,value7,value8

yellow,blue,value10,value11,value12

actual result实际结果

col1,col2,col3,col4,col5

value1,value2,value3,value4,value5

NaN,NaN,value6,value7,value8

NaN,NaN,value10,value11,value12

Initial problem -> Solved初始问题-> 已解决

I am getting an error like this:我收到这样的错误:

pandas.errors.ParserError: Error tokenizing data. C error: Expected 5 fields in line 3, saw 6

You have 2 empty columns in lines 3 and 4. So, that makes it 6 columns but you have 5 headers.您在第 3 行和第 4 行中有 2 个空列。因此,它有 6 个列,但您有 5 个标题。 Just delete the first comma like this:只需像这样删除第一个逗号:

,value6,value7,value8,value9

For the second question you can try this:对于第二个问题,你可以试试这个:

df['col1'] = df['col1'].fillna('yellow')

The topmost row of your csv file if the headers and it will determine the number of columns in your data.如果标题是 csv 文件的最上面一行,它将确定数据中的列数。 As you have 5 headers,因为你有 5 个标题,

col1, col2, col 3, col 4, col 5 col1, col2, col 3, col 4, col 5

The csv reader will expect there to be 5 columns in every subsequent row of data. csv 阅读器预计在随后的每一行数据中有 5 列。 As such, your 3rd and 4th row in your csv will be invalid and unalbe to be read by the csv reader.因此,csv 中的第 3 行和第 4 行将无效并且无法被 csv 阅读器读取。

Eg.例如。 ,,value6,value7,value8,value9 has a total of 6 columns. ,,value6,value7,value8,value9 共有 6 列。

This is what I need.这就是我需要的。

data = pd.read_csv(file)
hello = data.append({'col1': yello, 'col2': 'blue'}, ignore_index=True)

Now I'd need to write it again to the file.现在我需要将它再次写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM