I'm using this code:
for each in x:
descrVar = descrVar + " " + df.iloc[counter,each]
to iterate through a table and concatenate cells into a variable. The problem is, some of the cells will be a Nan. As a result, I'm getting the following error:
TypeError: can only concatenate str (not "numpy.float64") to str
I assume this means a Nan is a float64 and not a str. Is there any way around this, such as forcing every cell to convert to a str?
An f-string will format your data as str.
for each in x:
descrVar += f" {df.iloc[counter,each]}"
Related
import time
for i in range(2,6):
start = time.time()
n=i
end = time.time()
print(n)
time_cost=end-start
print(type(time_cost))
print('totally cost for '+n+'*'+n,str(time_cost))
I use str to change type for time_cost, but still have error
The problem here is that n is still an int when you try to concatenate it with 'totally cost for '
You must replace the last print statement with this:
print('totally cost for '+str(n)+'*'+str(n), str(time_cost))
It is also fine if you don't call str() on time_cost since it is a different parameter and thus print() automatically converts it. n is not converted since is concatenated explicitly using a + operator.
So final print can be:
print('totally cost for '+str(n)+'*'+str(n), time_cost)
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.
I'm receiving an error when I try to concatenate weight_kg with a string "kilograms". I want the numerical output of weight_kg followed by the string "kilograms". E.g. '50 kilograms'.
I've tried to convert weight_kg as a string so I can concatenate it to the string "kilograms"
weight_lbs = input("Weight (lbs): ")
weight_kg = int(weight_lbs) * 0.45
print(weight_kg)
print(weight_kg) + str("kilograms")
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
print(weight_kg) + str("kilograms")
you were trying to add the return value of a print() (which is a NoneType) to a string. They can't be added together.
You might want something like this:
print( str(weight_kg) + "kilograms" )
which converts weight_kg to string first then concatenates and prints the result out.
Try this:
print(str(weight_kg)+"kilograms")
no need to cast a string again to string,
and use str(object) to case object to str (like integer at your case)
and print function syntax is print("things to be printed <must be inside the () of the print func>")
to concatenate a 2 strings just str3=str_1+str_2
so according to this all :
print(str(weight_kg)+"kilograms")
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
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