Can I change the value of NaN from table to zero?

Asked 2 years ago, Updated 2 years ago, 61 views

import pandas as pd

import requests

from bs4 import BeautifulSoup

from tabulate import tabulate

import time

url = "http://comp.fnguide.com/SVO2/ASP/SVD_Main.asp?pGB=1&gicode=A005390&cID=&MenuYn=Y&ReportGB=&NewMenuID=101&stkGb=701"

res = requests.get(url)

soup = BeautifulSoup(res.content, "lxml")

table = soup.find_all("table")

df = pd.read_html(str(table))

print(df[4])

ja = df[4].iloc[4, 2]

The value represented by NaN in this data frame is I want to call it an integer value of 0. I don't know exactly what NaN is, so I don't know what to do. I'd appreciate it if you could answer.~~

Just in case, from what I checked on the developer mode on the site, The space corresponding to NaN is

& nbsp;

There's actually no space between & nbsp;, but if you type according to the site, the writing won't work...)

It comes out as.

nan

2022-09-20 19:04

1 Answers

If you want to change all NaN values in the data frame to zero in Pandas, you can do the following.

df.fillna(0)

Please refer to the code below.

import pandas as pd

import requests

from bs4 import BeautifulSoup

url = "http://comp.fnguide.com/SVO2/ASP/SVD_Main.asp?pGB=1&gicode=A005390&cID=&MenuYn=Y&ReportGB=&NewMenuID=101&stkGb=701"

res = requests.get(url)

soup = BeautifulSoup(res.content, "lxml")

table = soup.find_all("table")

df = pd.read_html(str(table))

print(df[4])

for i in range(len(df)):
    df[i].fillna(0,inplace=True)

print(df[4])

-Result


2022-09-20 19:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.