[英]Pandas read_csv fillna
I have some data that I am reading from a CSV file and one data frame column is recorded on a different time stamp interval (time series data) and I cant get a df.fillna(method = 'ffill').fillna(method = 'bfill')
to work. 我有一些数据,我正在读取CSV文件,一个数据帧列记录在不同的时间戳间隔(时间序列数据),我不能得到一个df.fillna(method = 'ffill').fillna(method = 'bfill')
工作。
If I don't read the CSV file with a keep_default_na=False
Python fills the gaps with a NaN but I would like the gaps to be blank so I can use the df.fillna(method = 'ffill')
如果我没有使用keep_default_na=False
读取CSV文件,则Python填补了NaN的空白,但我希望这些空白是空白的,所以我可以使用df.fillna(method = 'ffill')
import pandas as pd
import numpy as np
#read CSV file
df_raw = pd.read_csv('C:\\desktop\\combinedSP.csv', index_col='Date', parse_dates=True, keep_default_na=False)
df_raw.head()
df_raw2 = df_raw.fillna(method = 'ffill').fillna(method = 'bfill')
df_raw2.head()
It seems like no matter what I attempt I am not fixing the issue on the column labeled OAT
:( 似乎无论我尝试什么,我都没有在标有OAT
的列上解决问题:(
Any tips greatly appreciated, I have the data CSV file here loaded into my GitHub account. 任何提示大为赞赏,我有CSV文件在这里加载到我的GitHub的帐户。
When you do keep_default_na=False
this means that what read_csv
usually would read and parse to NaN it will no longer : 当你执行keep_default_na=False
这意味着read_csv
通常会读取并解析为NaN,它将不再:
By default the following values are interpreted as NaN:
'', '#N/A', '#N/AN/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan', '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'n/a', 'nan', 'null'
. 默认情况下,以下值被解释为NaN:'', '#N/A', '#N/AN/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan', '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'n/a', 'nan', 'null'
。
In this case, it's not parsing the empty string ''
as NaN, it's keeping them as the empty string. 在这种情况下,它不会将空字符串''
解析为NaN,而是将它们保留为空字符串。
Drop that kwarg and the fillnas ought to work. 放下那个kwarg和fillnas应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.