I have a csv file and I have to clean data.The problem is that I can fill the empty values by df.fillna() but there are some continious numbers in string which needs to be converted to float or int for further calculations.
I tried couple of methods but cannot find a solution.
Kindly help as I am new in data science field and maybe have made some mistake asking the question.
This column has an string value : df['hum'][316] = '64.70'
type(df['hum'][316]) = str
I stored the string value to a variable and then used float(value) but it gives an error.
value = df['hum'][316]
>>>' "64.70"'
type(value)
>>> str
float(value)
>>>ValueError: could not convert string to float: ' "64.70"'
ValueError: could not convert string to float: ' "64.70"'
df['hum'][316] = float(df['hum'][316])
seems the problems is the " inside the string, remove it by using a regular expression
import re
value = df['hum'][316]
value=re.sub('"','',value)
float(value)
instead of float possible to check also astype for cast operations applying on the whole df or series (column)
if you want to change the whole column of df to a float try:
df['hum'] = df['hum'].str.replace('"', '')
df['hum']=df['hum'].astype('float')
regards giulio
Related
Hi All: I receive a decimal in string format and want to convert it to Integer in groovy. could not find any solution so far. For eg: I get the string value as "100.0" and I need the output as 100. Please help.
I plan to run this groovy script in boomi.
First convert to float, then to integer:
def s = "100.0"
def f = s.toFloat()
def i = f.toInteger() //or (int)f
or one line,
def i = "100.0".toFloat().toInteger()
I want to convert different datatypes in age column to integer type as we can see the string, Na, and numeric type.
I tried the below code but isn't worked
train['age'].unique()
train.age =train.age.str.replace(' ', '')
Use:
#converting to numeric
train.age = pd.to_numeric(train.age.str.strip(), errors='coerce')
#remove non numeric values
train = train.dropna(subset=['age'])
#convert to integers
train.age = train.age.astype(int)
Given question - Given a list of 10 numbers, find the average of all such numbers which is a multiple of 3
num = []
newlist = []
for i in range(1, 11):
num.append(input())
for i in num:
if i%3==0:
newlist.append(i)
length = len(newlist)
total = sum(newlist)
average = total/length
print(average)
Getting this type error below at line 9 i.e. if i%3==0
not all arguments converted during string formatting
input() returns a string, so i%3 will actually perform printf-style string formatting. Since your input doesn't have any formatting specifiers, but the % operator's right operand is not empty, you get the error, because you attempted to format a sting that doesn't have enough formatting specifiers.
To solve this, convert your input to integers:
num.append(int(input()))
When you num.append(input()), the value of input() is a string. You need to first convert that to an int and handle any possible errors before continuing. One way to do this is to change it to:
num.append(int(input()))
Since all the values in num are strings, i % 3 tries to perform old-string formatting, which is not what you expect.
My code is as follows:
First I create a zero matrix:
ww = (64,8)
tt14=np.zeros(ww)
This is a float matrix. When I want to assign a string value (Date) to the matrix, it gives me this error:
ValueError: could not convert string to float: '6/29/2006'
Where is the problem?
The matrix expects Float types and the date is a type of String. You can convert the date to float by converting it into a timestamp first:
import time
date_str = "6/29/2006"
time_tuple = time.strptime(date_str, "%m/%d/%Y")
timestamp = time.mktime(time_tuple)
Result print timestamp:
1151503200.0
On retrieval we can convert the timestamp back to string:
dt_obj = datetime.fromtimestamp(timestamp)
date_str = dt_obj.strftime("%m/%d/%Y")
Result print date_str:
06/29/2006
Or as pointed out in the comments
If the array is only used for string values you can pass a data type parameter to numpy.zeros() to populate the matrix with empty strings:
ww = (64,8)
tt14=np.zeros(ww, dtype = str)
Now you can add any string value to the matrix.
I want to create a binary number in matlab and am having difficulty concatenating the numbers.
Here's what I tried so far:
testarray = zeros(10,10)
testarray = num2str(testarray) % Convert all values to type string
testarray(1,1) = num2str(1); % Fill with abitrary value
testarray(1,1) = strcat(testarray(1,1), num2str(0)); % Trying to make '10' here but instead I get this error: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
Any help would be appreciated.
In your example, the problem is that '10' has size [1,2], but testarray(1,1) has size [1,1]. So you might consider using cells instead:
testarray = cell(5,5);
testarray{1,1} = strcat(testarray(1,1), num2str(0));
By the way, you should have a look at the function dec2bin.
From the documentation:
dec2bin(23)
ans =
10111
The resulting value is a string.
So if you want to concatenate two binary values (encoded as strings), just do:
['10' '11']
ans =
1011