我的问题与此one有关。我有一个名为“ test.csv”的文件,其中“ NA”是region
的值。我想读为“ NA”而不是“ NaN”。但是,test.csv的其他列中缺少值,我想保留为“ NaN”。我怎样才能做到这一点?
# test.csv looks like this:
这是我尝试过的:
import pandas as pd
# This reads NA as NaN
df = pd.read_csv(test.csv)
df
region date expenses
0 NaN 1/1/2019 53
1 EU 1/2/2019 NaN
# This reads NA as NA, but doesn't read missing expense as NaN
df = pd.read_csv('test.csv', keep_default_na=False, na_values='_')
df
region date expenses
0 NA 1/1/2019 53
1 EU 1/2/2019
# What I want:
region date expenses
0 NA 1/1/2019 53
1 EU 1/2/2019 NaN
添加参数keep_default_na=False
的问题是expenses
的第二个值不能作为NaN
读入。因此,如果我再尝试pd.isnull(df['value'][1])
,则会返回为False
。
答案 0 :(得分:1)
对我来说,这可行:
df = pd.read_csv('file.csv', keep_default_na=False, na_values=[''])
给出:
region date expenses
0 NA 1/1/2019 53.0
1 EU 1/2/2019 NaN
但是由于其他列中可能还有其他NaN
,我宁愿安全起见,
df = pd.read_csv('file.csv')
df['region'] = df['region'].fillna('NA')
答案 1 :(得分:0)
在指定keep_default=False
时,所有默认值均不视为nan,因此您应指定它们:
使用keep_default_na=False, na_values= [‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NULL’, ‘NaN’, ‘n/a’, ‘nan’, ‘null’]
答案 2 :(得分:0)
这种方法对我有用:
import pandas as pd
df = pd.read_csv('Test.csv')
co1 col2 col3 col4
a b c d e
NaN NaN NaN NaN NaN
2 3 4 5 NaN
我复制了该值并创建了一个列表,默认情况下将其解释为 NaN,然后注释掉我想解释为非 NaN 的 NA。这种方法仍然将除 NA 之外的其他值视为 NaN。
#You can also create your own list of value that should be treated as NaN and
# then pass the values to na_values and set keep_default_na=False.
na_values = ["",
"#N/A",
"#N/A N/A",
"#NA",
"-1.#IND",
"-1.#QNAN",
"-NaN",
"-nan",
"1.#IND",
"1.#QNAN",
"<NA>",
"N/A",
# "NA",
"NULL",
"NaN",
"n/a",
"nan",
"null"]
df1 = pd.read_csv('Test.csv',na_values=na_values,keep_default_na=False )
co1 col2 col3 col4
a b c d e
NaN NA NaN NA NaN
2 3 4 5 NaN