Python ValueError: could not convert string to float - python

I have the code below, where the input is h_ply =['0.12, 0.15, 0.2, 0.125']
h_ply = simpledialog.askstring('Laminate Properties','Ply Thickness')
try:
h_layer_list = [int(x) for x in h_ply.split(',')]
h_layer = np.array(h_layer_list) * 0.001
I have also tried
h_ply1 = np.array(h_ply)
h_layer = h_ply1.astype(np.float)
But got the same error.
I am trying to get h_ply list into a np.array of Floats. But a ValueError appears.
I understand this could be because I should have '' every number and not just at the ends? But ideally I want to avoid that to be more user friendly. What is the best way to get rid of this Error message?
Thank you

The way to deal with this is to properly access the string. Look at your data:
h_ply =( # a tuple, containing ...
[ # a single element, a list, containing ...
[ # a single element, a list, containing ...
'0.12, 0.15, 0.2, 0.125' # ... a string that you have to split
]
]
)
You have to peel off all of those layers to get to the string:
h_str = h_ply[0][0][0]
Now you can split the string and convert the elements to numeric values. The code you posted is trying to split the tuple and convert it.

Related

Extract value in specific range

I have one dataset with several column:
data-pioggia-name.....
I would like to get values, within of the column pioggia, between 0 and 400.
I tried with:
start='0'
end='400'
data = (data['pioggia']>start)&(data['pioggia']<=end)
but I have error: ">" not supported between instances of 'str' and 'int'
I tried also:
data = data['pioggia'].between(0,400, inclusive=True)
but I have the same error.
There is some solution? also for example with replace?
Try adding this line:
data['pioggia'] = data['pioggia'].astype(int)
Also, make your start and end variables be ints (e.g. 0) instead of strings (e.g. '0').
Like this:
start = 0 # Notice this and `end` are ints, not strings
end = 400
data['pioggia'] = data['pioggia'].astype(int)
data = (data['pioggia']>start)&(data['pioggia']<=end)

Change only one element in list to float or other transformation

Let say I have this data:
[[u'alpha',u'0.1'],[u'bravo',u'0.2']]
What I want to achieve is to change the 2nd element for each item to float
Here is my code. It able to get the output that I want, but I'm looking for something simpler, and more generic. For example, the element might be 99th out of 100, or I want to change the first element to title case.
I'm exploring map and lambda but cannot see how to use it in this case.
#!/bin/env python
data = [[u'alpha',u'0.1'],[u'bravo',u'0.2']]
print data
tgb=[]
for item in data:
rfv=[]
for x,elem in enumerate(item):
if x == 1:
rfv.append(float(elem))
else:
rfv.append(elem)
tgb.append(rfv)
print tgb
Output:
[[u'alpha', u'0.1'], [u'bravo', u'0.2']]
[[u'alpha', 0.1], [u'bravo', 0.2]]
You could do the following:
def convert(x):
try:
return float(x)
except ValueError, e:
return x.title()
data = [[u'alpha',u'0.1'], [u'bravo',u'0.2'], [u'charlie', u'0.1', u'0.2', u'0.3', u'0.4']]
data = [[convert(element) for element in entry] for entry in data]
print data
This will attempt to convert all items to floats, but leave them as strings if they cannot be converted in title format. This would display the following output:
[[u'Alpha', 0.1], [u'Bravo', 0.2], [u'Charlie', 0.1, 0.2, 0.3, 0.4]]
This would also work for 100 elements. Note, it would not be possible to use a lambda for the convert() function as it would need to be a single expression.
Alternatively, map() could be used to give the same results:
data = [map(convert, entry) for entry in data]
Let's solve with map, lambda and slicing as you mentioned in your question. If you know at which index you want to modify, you can do:
print map(lambda x,index=1:x[:index]+[float(x[index])]+x[index+1:] ,s)
Note that, I have used 1 as default value of index.
Demo:
>>s=[[u'alpha',u'0.1',u'0.1'],[u'bravo',u'0.2',u'0.1',u'0.1']]
>>print map(lambda x,index=1 : x[:index] + [float(x[index])] + x[index+1:] , s)
[[u'alpha', 0.1, u'0.1'], [u'bravo', 0.2, u'0.1', u'0.1']]

can't convert string to integer-python

I have a string within a tuple like this:
params': {
'rtinseconds': '57.132',
**'charge': '3+'**,
'pepmass': (822.6547241, None),
title': '20130630_006.d, MS/MS of 822.6547241 3+ at 0.9522 mins'
}
I am trying to read and convert the value of charge '3+' to integer value 3.
I tried the following code where I read the first character in the string and stored it in a separate variable, then tried to convert it to int, but does not work. The type of 3 is still str. Does anyone have any suggestions?
temp_z = item['params']['charge']
z = temp_z[0:1]
str(z)
int(z)
In the simple case:
z = int(params['charge'].replace('+',''))
However if it is possible that your item may have a negative charge you may want:
if '+' in params['charge']:
z = int(params['charge'].replace('+',''))
else:
z = -int(params['charge'].replace('-',''))

Splitting json data in python

I'm trying to manipulate a list of items in python but im getting the error "AttributeError: 'list' object has no attribute 'split'"
I understand that list does not understand .split but i don't know what else to do. Below is a copy paste of the relevant part of my code.
tourl = 'http://data.bitcoinity.org/chart_data'
tovalues = {'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all','compare':'no','data_type':'price_volume','chart_type':'line_bar','smoothing':'linear','chart_types':'ccacdfcdaa'}
todata = urllib.urlencode(tovalues)
toreq = urllib2.Request(tourl, todata)
tores = urllib2.urlopen(toreq)
tores2 = tores.read()
tos = json.loads(tores2)
tola = tos["data"]
for item in tola:
ting = item.get("values")
ting.split(',')[2] <-----ERROR
print(ting)
To understand what i'm trying to do you will also need to see the json data. Ting outputs this:
[
[1379955600000L, 123.107310846774], [1379959200000L, 124.092526428571],
[1379962800000L, 125.539504822835], [1379966400000L, 126.27024617931],
[1379970000000L, 126.723474983766], [1379973600000L, 126.242406356837],
[1379977200000L, 124.788410570987], [1379980800000L, 126.810084904632],
[1379984400000L, 128.270580796748], [1379988000000L, 127.892411269036],
[1379991600000L, 126.140579640523], [1379995200000L, 126.513705084746],
[1379998800000L, 128.695124951923], [1380002400000L, 128.709738051044],
[1380006000000L, 125.987767097378], [1380009600000L, 124.323433535528],
[1380013200000L, 123.359378559603], [1380016800000L, 125.963250678733],
[1380020400000L, 125.074618194444], [1380024000000L, 124.656345088853],
[1380027600000L, 122.411303435449], [1380031200000L, 124.145747100372],
[1380034800000L, 124.359452274881], [1380038400000L, 122.815357211394],
[1380042000000L, 123.057706915888]
]
[
[1379955600000L, 536.4739135], [1379959200000L, 1235.42506637],
[1379962800000L, 763.16329656], [1379966400000L, 804.04579319],
[1379970000000L, 634.84689741], [1379973600000L, 753.52716718],
[1379977200000L, 506.90632968], [1379980800000L, 494.473732950001],
[1379984400000L, 437.02095093], [1379988000000L, 176.25405034],
[1379991600000L, 319.80432715], [1379995200000L, 206.87212398],
[1379998800000L, 638.47226435], [1380002400000L, 438.18036666],
[1380006000000L, 512.68490443], [1380009600000L, 904.603705539997],
[1380013200000L, 491.408088450001], [1380016800000L, 670.275397960001],
[1380020400000L, 767.166941339999], [1380024000000L, 899.976089609997],
[1380027600000L, 1243.64963909], [1380031200000L, 1508.82429811],
[1380034800000L, 1190.18854705], [1380038400000L, 546.504592349999],
[1380042000000L, 206.84883264]
]
And ting[0] outputs this:
[1379955600000L, 123.187067936508]
[1379955600000L, 536.794013499999]
What i'm really trying to do is add up the values from ting[0-24] that comes AFTER the second comma. This made me try to do a split but that does not work
You already have a list; the commas are put there by Python to delimit the values only when printing the list.
Just access element 2 directly:
print ting[2]
This prints:
[1379962800000, 125.539504822835]
Each of the entries in item['values'] (so ting) is a list of two float values, so you can address each of those with index 0 and 1:
>>> print ting[2][0]
1379962800000
>>> print ting[2][1]
125.539504822835
To get a list of all the second values, you could use a list comprehension:
second_vals = [t[1] for t in ting]
When you load the data with json.loads, it is already parsed into a real list that you can slice and index as normal. If you want the data starting with the third element, just use ting[2:]. (If you just want the third element by itself, just use ting[2].)

PYTHON problem with negative decimals

I have a list of negative floats. I want to make a histogram with them. As far as I know, Python can't do operations with negative numbers. Is this correct? The list is like [-0.2923998, -1.2394875, -0.23086493, etc.]. I'm trying to find the maximum and minimum number so I can find out what the range is. My code is giving an error:
setrange = float(maxv) - float(minv)
TypeError: float() argument must be a string or a number
And this is the code:
f = open('clusters_scores.out','r')
#first, extract all of the sim values
val = []
for line in f:
lineval = line.split()
print lineval
val.append(lineval)
print val
#val = map(float,val)
maxv = max(val)
minv = min(val)
setrange = float(maxv) - float(minv)
All the values that are being put into the 'val' list are negative decimals. What is the error referring to, and how do I fix it?
The input file looks like:
-0.0783532095182 -0.99415440702 -0.692972552716 -0.639273674023 -0.733029194040.765257900121 -0.755438339963
-0.144140594077 -1.06533353638 -0.366278118372 -0.746931508538 -1.02549039392 -0.296715961215
-0.0915937502791 -1.68680560936 -0.955147543358
-0.0488457137771 -0.0943080192383 -0.747534412969 -1.00491121699
-1.43973471463
-0.0642611118901 -0.0910684525497
-1.19327387414 -0.0794696449245
-1.00791366035 -0.0509749096549
-1.08046507281 -0.957339914505 -0.861495748259
The results of split() are a list of split values, which is probably why you are getting that error.
For example, if you do '-0.2'.split(), you get back a list with a single value ['-0.2'].
EDIT: Aha! With your input file provided, it looks like this is the problem: -0.733029194040.765257900121. I think you mean to make that two separate floats?
Assuming a corrected file like this:
-0.0783532095182 -0.99415440702 -0.692972552716 -0.639273674023 -0.733029194040 -0.765257900121 -0.755438339963
-0.144140594077 -1.06533353638 -0.366278118372 -0.746931508538 -1.02549039392 -0.296715961215
-0.0915937502791 -1.68680560936 -0.955147543358
-0.0488457137771 -0.0943080192383 -0.747534412969 -1.00491121699
-1.43973471463
-0.0642611118901 -0.0910684525497
-1.19327387414 -0.0794696449245
-1.00791366035 -0.0509749096549
-1.08046507281 -0.957339914505 -0.861495748259
The following code will no longer throw that exception:
f = open('clusters_scores.out','r')
#first, extract all of the sim values
val = []
for line in f:
linevals = line.split()
print linevals
val += linevals
print val
val = map(float, val)
maxv = max(val)
minv = min(val)
setrange = float(maxv) - float(minv)
I have changed it to take the list result from split() and concatenate it to the list, rather than append it, which will work provided there are valid inputs in your file.
All the values that are being put into the 'val' list are negative decimals.
No, they aren't; they're lists of strings that represent negative decimals, since the .split() call produces a list. maxv and minv are lists of strings, which can't be fed to float().
What is the error referring to, and how do I fix it?
It's referring to the fact that the contents of val aren't what you think they are. The first step in debugging is to verify your assumptions. If you try this code out at the REPL, then you could inspect the contents of maxv and minv and notice that you have lists of strings rather than the expected strings.
I assume you want to put all the lists of strings (from each line of the file) together into a single list of strings. Use val.extend(lineval) rather than val.append(lineval).
That said, you'll still want to map the strings into floats before calling max or min because otherwise you will be comparing the strings as strings rather than floats. (It might well work, but explicit is better than implicit.)
Simpler yet, just read the entire file at once and split it; .split() without arguments splits on whitespace, and a newline is whitespace. You can also do the mapping at the same point as the reading, with careful application of a list comprehension. I would write:
with open('clusters_scores.out') as f:
val = [float(x) for x in f.read().split()]
result = max(val) - min(val)

Categories

Resources