将Nan值转换为零pandas Python

我想把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}})

Expected Output:

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}})
✅ 最佳回答:

将数据帧的所有NaN值替换为0-

df = df.fillna(0)