Numeric value as a string and convert to actual numeric - python-3.x

I found this thread how to make a variable change from the text "1m" into "1000000" in python
My string values are in a column within a pandas dataframe. The string/0bkects values are like 18M, 345K, 12.9K, 0, etc.
values = df5['Values']
multipliers = { 'k': 1e3,
'm': 1e6,
'b': 1e9,
}
pattern = r'([0-9.]+)([bkm])'
for number, suffix in re.findall(pattern, values):
number = float(number)
print(number * multipliers[suffix])
Running the code gives this error:
Traceback (most recent call last):
File "c:/Users/thebu/Documents/Python Projects/trading/screen.py", line 19, in <module>
for number, suffix in re.findall(pattern, values):
File "C:\Users\thebu\Anaconda3\envs\trading\lib\re.py", line 223, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
Thanks

Here's another way using regex:
import re
def get_word(s):
# find word
r = re.findall(r'[a-z]', s)
# find numbers
w = re.findall(r'[0-9]', s)
if len(r) > 0 and len(w) > 0:
r = r[0]
v = multipliers.get(r, None)
if v:
w = int(''.join(w))
w *= v
return round(w)
df['col2'] = df['col'].apply(get_word)
print(df)
col col2
0 10k 10000
1 20m 20000000
Sample Data
df = pd.DataFrame({'col': ['10k', '20m']})

Related

i am trying to calculate sentimental score of each syntax of csv file by using csv library

this is the error which i am getting. In the previous post i forget to put both function . In the first function i'm reading csv file and removing punctuation and send the string to second function to calculate the sentimentel score. this code give output for few row of csv file and then show this error i'm new in python
Traceback (most recent call last):
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 116, in <module>
Countswordofeachsyntax()
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 92, in Countswordofeachsyntax
print(findsentimentalscore(nopunct))
File "C:/Users/Public/Downloads/Hotelsurvey.py", line 111, in findsentimentalscore
ss =ss + weight
TypeError: unsupported operand type(s) for +: 'int' and 'list'
def Countswordofeachsyntax():
nopunct = ""
with open('dataset-CalheirosMoroRita-2017.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='|')
for sys in csv_reader:
for value in sys:
nopunct = ""
for ch in value:
if ch not in punctuation:
nopunct = nopunct + ch
print(findsentimentalscore(nopunct))
def findsentimentalscore(st):
ss = 0
count = len(st.split())
mycollapsedstring = ' '.join(st.split())
print(str(mycollapsedstring.split(' ')) + " := " + str(len(mycollapsedstring.split())))
for key, weight in keywords.items():
if key in mycollapsedstring.lower():
ss =ss + weight
#print(key, weight)
res = (ss / count * 100)
return math.ceil(res)

I want to print the dictionary in python

l = {}
name = [(str, input().split()) for i in range(0, 15)]
dob = [(int, input().split()) for i in range(0, 15)]
print({name[i]:dob[i] for i in range(len(dob))})
I want to print 15 items in a dictionary format of name as key and dateofbirth(dob) as value.What wrong I am doing?
.....................................................................................
the error is:
Traceback (most recent call last):
File "main.py", line 4, in <module>
print({name[i]:dob[i] for i in range(len(dob))})
File "main.py", line 4, in <dictcomp>
print({name[i]:dob[i] for i in range(len(dob))})
TypeError: unhashable type: 'list'
The issue is not in the print() function but in the way you make up the first list: instead of pulling out the names, it gives you a (<class 'str'>, 'string') tuple that cannot be used as a key for a dictionary. The same happens with the 'dob' variable, but the issue is only with keys.
Try doing:
name = [input() for i in range(0, 15)] #this takes and returns the input. no need to convert to string
dob = [int(input()) for i in range(0, 15)] #this takes an input and returns it's numeric value
I would do it like this (this returns a generator):
name = map(str, input().split())
dob = map(int, input().split())
print({n: d for n, d in zip(name, dob)})
If you want it to return a list instead:
name = list(map(str, input().split()))
dob = list(map(int, input().split()))
print({n: d for n, d in zip(name, dob)})

Write a program to take dictionary from the keyboard and print sum of values?

d =dict(input('Enter a dictionary'))
sum = 0
for i in d.values():
sum +=i
print(sum)
outputs: Enter a dictionary{'a': 100, 'b':200, 'c':300}
this is the problem arises:
Traceback (most recent call last):
File "G:/DurgaSoftPython/smath.py", line 2, in <module>
d =dict(input('Enter a dictionary'))
ValueError: dictionary update sequence element #0 has length 1; 2 is required
You can't create a dict from a string using the dict constructor, but you can use ast.literal_eval:
from ast import literal_eval
d = literal_eval(input('Enter a dictionary'))
s = 0 # don't name your variable `sum` (which is a built-in Python function
# you could've used to solve this problem)
for i in d.values():
s +=i
print(s)
Output:
Enter a dictionary{'a': 100, 'b':200, 'c':300}
600
Using sum:
d = literal_eval(input('Enter a dictionary'))
s = sum(d.values())
print(s)
import json
inp = input('Enter a dictionary')
inp = dict(json.loads(inp))
sum = sum(inp.values())
print(sum)
input Enter a dictionary{"a": 100, "b":200, "c":300}
output 600
Actually the return of input function is a string. So, in order to have a valid python dict you need to evaluate the input string and convert it into dict.
One way to do this can be done using literal_eval from ast package.
Here is an example:
from ast import literal_eval as le
d = le(input('Enter a dictionary: '))
_sum = 0
for i in d.values():
_sum +=i
print(_sum)
Demo:
Enter a dictionary: {'a': 100, 'b':200, 'c':300}
600
PS: Another way can be done using eval but it's not recommended.

It the any proper way how to take a two byte elemetns from list, concat them, and convert them to integer

I have a list with 4 element which contain integers
data = [134, 2, 4, 170]
hexdata = [0x86, 0x2, 0x4, 0xAA]
i need to get hex data from two last elements eg. (0x04 and 0xAA)
concatenate them to this view 0x04AA and convert to int
in the end i need to get integer with value = 1194.
i am stuck in this task/
data = [134, 2, 4, 170]
for x in data:
print("0x%x" % (x), end=" ")
print()
c = "0x%x%x" % (data[2], data[3])
print(c)
print(int(c))
Traceback (most recent call last):
File "123.py", line 7, in <module>
print(int(c))
ValueError: invalid literal for int() with base 10: '0x4aa'
You don't need to bother with string formatting here - use int.from_bytes instead, eg:
data = [134, 2, 4, 170]
res = int.from_bytes(data[-2:], 'big')
# 1194
data = [134, 2, 4, 170]
result = data[-2] << 8 | data[-1]
Simply multiply the 4 accordingly and add it up? No need to go hex on it ...
data = [134, 2, 4, 170]
rv = data[2]*256 + data[3] # 0x04AA == 0x04*256 + 0xAA
print(rv)
Output:
1194

<__main__.CloudTravel object at 0x000000D30F609828> is not iterable

.py
a = (0,0,70)
b = (90,0,45)
c = ("1 2","0 2","0 1")
d = 0
e = 1
class Travel(object):
def trip(self,latitude,longitude,canTravel,origin,destination):
self.latitude = latitude
self.longitude = longitude
self.canTravel = canTravel
self.origin = origin
self.destination = destination
print(latitude)
print(longitude)
print(origin)
print(destination)
print(canTravel)
for c in canTravel:
print(c)
pass
c = Travel()
c.trip(a,b,c,d,e)
output
(0, 0, 70)
(90, 0, 45)
0
1
<__main__.Travel object at 0x000000E836D25898>
Traceback (most recent call last):
File ".\shortestpath.py", line 26, in <module>
c.trip(a,b,c,d,e)
File ".\shortestpath.py", line 20, in trip
for c in canTravel:
TypeError: 'Travel' object is not iterable
I want to pass the tuple of string ("1 2","0 2","0 1") to the trip function, and want to iterate over it.
How do I iterate over the <main.Travel object at 0x000000E836D25898>

Resources