I have a question regarding the data type.

Asked 2 years ago, Updated 2 years ago, 47 views

Hello, everyone I'm a beginner at coding.

I'm getting data from the website Part of the data is contained in non-numeric characters.

For example, all other data are float types, such as 2.4 and 12.7, but sometimes the data contains strings such as the expression "capital erosion". Is there a way to make this a number zero?

def strcheck(a):

    if a.isalpha():
        return False
    elif a.isdigit():
        return True


if strcheck(roe) == False:

    decision_list_2 = [code, "insufficient data", "insufficient data",]   
else:

    roe = float(roe)

The actual code I made is the same way as above, but there is no problem if the data I put is text, but if the data goes into numbers

'numpy.float64' object has no attribute 'isalpha'

I got this error.

I'd appreciate your help.

I can upload the entire code if you need it.

python pandas crawling

2022-09-20 18:57

1 Answers

There are float type and str type data together in one list, and if you want to change str type data to float type value 0, you can do it as below.

x=[1.3, 1.5, "Insufficient data", 3.3, "Insufficient data"]

print(x)

for i in range(len(x)):
    if type(x[i])==str:
        x[i]=0.0     

print(x)

Or

x=[1.3, 1.5, "Insufficient data", 3.3, "Insufficient data"]

print(x)

x=[0.0 if type(val)==str else val for val in x]

print(x)


2022-09-20 18:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.