我想将nan
值变为零,并获得预期输出。
import pandas as pd
data = pd.DataFrame({'Symbol': {4: 'DIS', 5: 'DKNG', 6: 'EXC'},
'Number of Buy s': {4: 1.0, 5: 2.0, 6: 1.0},
'Number of Cover s': {4: nan, 5: 2.0, 6: nan},
'Number of Sell s': {4: 1.0, 5: 1.0, 6: 1.0},
'Number of Short s': {4: nan, 5: 1.0, 6: nan},
'Gains/Losses': {4: -47.700000000000045, 5: -189.80000000000018, 6: 11.599999999999909},
'Percentage change': {4: -1.9691362018764154, 5: 1.380299604344981, 6: -2.006821924253117}})
预期输出:
data = pd.DataFrame({'Symbol': {4: 'DIS', 5: 'DKNG', 6: 'EXC'},
'Number of Buy s': {4: 1.0, 5: 2.0, 6: 1.0},
'Number of Cover s': {4: 0, 5: 2.0, 6: 0},
'Number of Sell s': {4: 1.0, 5: 1.0, 6: 1.0},
'Number of Short s': {4: 0, 5: 1.0, 6: 0},
'Gains/Losses': {4: -47.700000000000045, 5: -189.80000000000018, 6: 11.599999999999909},
'Percentage change': {4: -1.9691362018764154, 5: 1.380299604344981, 6: -2.006821924253117}})
用0-替换DataFrame的所有NaN值
df = df.fillna(0)
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4))
df.iloc[1, 1] = np.nan
df.iloc[1, 2] = np.nan
df.iloc[1, 3] = np.nan
df.iloc[2, 0] = np.nan
df = df.fillna(0)
print(df)