Error while processing print() in console - python

Here is some code on python 3 using Pandas, interpreter is 3.4.1
import pandas as pd
pd.set_option('display.width', 300)
file = open('C:\PythonData\Result.csv', 'w')
file.write('"StopFirstIndex"' + ';' + '"StopLastIndex"' + ';' \
+ '"GOV_NUMBER"' + ';' + '"StopTime"' + '\n')
df1 = pd.read_csv('C:\PythonData\data4.csv', ",", header=0).sort_index(by=['CAR_ID', 'DATE_TO'])
df1.index = range(0, len(df1))
StopFirstIndex = 0
StopLastIndex = 0
CntZeroSpeedRows = 0
for index, row in df1.iterrows():
if row['SPEED'] == df1.get_value(index + 1, 'SPEED') == 0 and row['CAR_ID'] == df1.get_value(index + 1, 'CAR_ID'):
if CntZeroSpeedRows >= 1:
CntZeroSpeedRows += 1
else:
CntZeroSpeedRows = 1
else:
if CntZeroSpeedRows >= 3:
StopFirstIndex = index - CntZeroSpeedRows
StopLastIndex = index
StopTime = pd.to_datetime(df1.get_value(StopLastIndex, 'DATE_TO'), dayfirst=True) - \
pd.to_datetime(df1.get_value(StopFirstIndex, 'DATE_TO'), dayfirst=True)
tempdf1 = pd.concat([df1[StopFirstIndex:StopLastIndex+1]['LATITUDE'],
df1[StopFirstIndex:StopLastIndex+1]['LONGITUDE']], axis=1)
file.write('"' + str(StopFirstIndex) + '";"' + str(StopLastIndex) + '";"' \
+ str(df1.get_value(StopFirstIndex, 'GOV_NUMBER')) + '";"' + str(StopTime) + '"' + '\n') # making csv
print(StopFirstIndex, StopLastIndex, df1.get_value(StopFirstIndex, 'GOV_NUMBER'), StopTime) # printing results in console
CntZeroSpeedRows = 0
file.close()
Making of the csv file is ok, but if I print results in console, after printing correct results, it shows me following error:
> Traceback (most recent call last):
> File "C:/Users/Moiseev/PycharmProjects/untitled1/test1.py", line 21, in <module>
> if row['SPEED'] == df1.get_value(index + 1, 'SPEED') == 0 and row['CAR_ID'] == df1.get_value(index + 1, 'CAR_ID'):
> File "C:\Users\Moiseev\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1542, in get_value
return engine.get_value(series.values, index)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas\index.c:2993)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas\index.c:2808)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas\index.c:3534)
File "hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7035)
File "hashtable.pyx", line 387, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6976) KeyError: 1000
> Process finished with exit code 1
So, there is two questions:
1. Is it critical?
2. How can I remove this error?

Related

Passing an IF statement with several elif branches using multiple columns to a Pandas dataframe

Here is a snapshot of my dataframe (mrp): https://i.stack.imgur.com/bWRGp.png
I am trying to apply the following logic to each row and save the result into a new column:
def planning_period(x):
if x <= 30:
return mrp['p01'] + mrp['p02']
elif x <= 60:
return mrp['p01'] + mrp['p02'] + mrp['p03']
elif x <= 90:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04']
elif x <= 120:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05']
elif x <= 150:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06']
elif x <= 180:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06'] + mrp['p07']
elif x <= 210:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06'] + mrp['p07'] + mrp['p08']
elif x <= 240:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06'] + mrp['p07'] + mrp['p08'] + mrp['p09']
elif x <= 270:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06'] + mrp['p07'] + mrp['p08'] + mrp['p09'] + mrp['p10']
else:
return mrp['p01'] + mrp['p02'] + mrp['p03'] + mrp['p04'] + mrp['p05'] + mrp['p06'] + mrp['p07'] + mrp['p08'] + mrp['p09'] + mrp['p10'] + mrp['p11']
mrp['daily_consumption'] = mrp['rem_dys'].apply(planning_period)
Basically what I'm trying to achieve is this:
if the value of the cell in "rem_dys" column <= 30, then add "p01" + "p02" column values to a new cell in column "daily_consumption"
if the value of the cell in "rem_dys" column <= 60, then add "p01" + "p02" + "p03" column value to a new cell in column "daily_consumption"
so on and so forth until "rem_dys" column <= 270.
It's basically a 30 (days) step between each IF statement, representing every month in a year (took an average of 30 day for the sake of simplicity).
However, when i run this Python spits out the following message:
Traceback (most recent call last): File
"/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py",
line 3621, in get_loc
return self._engine.get_loc(casted_key) File "pandas/_libs/index.pyx", line 136, in
pandas._libs.index.IndexEngine.get_loc File
"pandas/_libs/index.pyx", line 163, in
pandas._libs.index.IndexEngine.get_loc File
"pandas/_libs/hashtable_class_helper.pxi", line 5198, in
pandas._libs.hashtable.PyObjectHashTable.get_item File
"pandas/_libs/hashtable_class_helper.pxi", line 5206, in
pandas._libs.hashtable.PyObjectHashTable.get_item KeyError:
'daily_consumption'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File
"/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/frame.py",
line 3799, in _set_item_mgr
loc = self._info_axis.get_loc(key) File "/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py",
line 3623, in get_loc
raise KeyError(key) from err KeyError: 'daily_consumption'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/grumpybear/Projects/python/alw-mrp/mrp-0.12.py", line 352, in
mrp['daily_consumption'] = mrp['rem_dys'].apply(planning_period) File
"/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/frame.py",
line 3645, in setitem
self._set_item_frame_value(key, value) File "/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/frame.py",
line 3788, in _set_item_frame_value
self._set_item_mgr(key, arraylike) File "/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/frame.py",
line 3802, in _set_item_mgr
self._mgr.insert(len(self._info_axis), key, value) File "/home/grumpybear/.local/lib/python3.8/site-packages/pandas/core/internals/managers.py",
line 1235, in insert
raise ValueError( ValueError: Expected a 1D array, got an array with shape (1688, 1688)
I know I'm doing something wrong (this is my first Python code) but don't know what or how to fix it...
It seems that the issue here is that you return (aggregated) columns instead of values here (e.g. mrp['p01'] + mrp['p02'] will be a pd.Series with the length of your DataFrame rather than the one value you actually want to have). So you won't be able to assign the returned value of the apply as it is not a list (1D array), but rather a 2D array.
So what you can do is to use the apply for each row and calculate the values per row instead of using the entire columns:
def planning_period(row: pd.Series) -> float:
if row["rem_dys"] <= 30:
return row['p01'] + row['p02']
elif row["rem_dys"] <= 60:
...
mrp['daily_consumption'] = mrp.apply(lambda row: planning_period(row), axis=1)

Pandas TypeError: can only concatenate str (not "int") to str

I have a problem where two almost identical html files create different behavior on the code. If i choose the HTML file in the first picture, pandasGUI will load the dataframe just fine. However if i choose second HTML file, it throws a typeerror as stated in the title. I have tried to fix this on and off for three weeks and i am completely lost. Can anyone help? Code below.
import pandas as pd
from scipy.interpolate import interp1d
from pandasgui import show
pd.set_option('display.max_columns', 100)
pd.set_option('precision', 3)
file = ''
def choose_file():
global file
i = input("Please choose an option below: \n"
"1: Own Team \n"
"2: Shortlist \n")
if (i == '1'):
file = './own_team.html'
elif (i == '2'):
file = './shortlist.html'
return file
file = choose_file()
attribute_view = pd.read_html(file)
map = interp1d([1, 7000], [1, 100])
df = attribute_view[0]
if(file == './shortlist.html'):
for column in ['Inf', 'Name', 'Age', 'Best Pos', 'Personality', 'Acc', 'Wor', 'Vis', 'Thr', 'Tec', 'Tea', 'Tck', 'Str', 'Sta', 'TRO', 'Ref', 'Pun', 'Pos', 'Pen', 'Pas', 'Pac', '1v1', 'OtB', 'Nat', 'Mar', 'L Th', 'Lon', 'Ldr', 'Kic', 'Jum', 'Hea', 'Han', 'Fre', 'Fla', 'Fir', 'Fin', 'Ecc', 'Dri', 'Det', 'Dec', 'Cro', 'Cor', 'Cnt', 'Cmp', 'Com', 'Cmd', 'Bra', 'Bal', 'Ant', 'Agi', 'Agg', 'Aer']:
df[column] = df[column].replace('-', 0)
print(df)
df['Team_dna'] = (df['Agg'] + df['Ant'] + df['Det'] + df['Tea'] + df['Wor'] + df['Acc'] + df['Sta'])
# Sweeper Keeper - Attack
df['sk_at'] = map((df['Aer'] + df['Com'] + df['Fir'] + df['Han'] + df['Pas'] + df['Ref'] + df['TRO'] + df['Thr'] + df['Cmp'] + df['Dec'] + df['Vis'] + df['Acc']) * 40)
EDIT:
Full traceback:
Please choose an option below:
1: Own Team
2: Shortlist
2
Traceback (most recent call last):
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 149, in na_arithmetic_op
result = expressions.evaluate(op, str_rep, left, right)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\computation\expressions.py", line 208, in evaluate
return _evaluate(op, op_str, a, b)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\computation\expressions.py", line 70, in _evaluate_standard
return op(a, b)
TypeError: can only concatenate str (not "int") to str
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Github/FM20-Analysis/FM.py", line 36, in <module>
df['sk_at'] = map((df['Aer'] + df['Com'] + df['Fir'] + df['Han'] + df['Pas'] + df['Ref'] + df['TRO'] + df['Thr'] + df['Cmp'] + df['Dec'] + df['Vis'] + df['Acc']) * 40)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
return method(self, other)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\__init__.py", line 503, in wrapper
result = arithmetic_op(lvalues, rvalues, op, str_rep)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 197, in arithmetic_op
res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 151, in na_arithmetic_op
result = masked_arith_op(left, right, op)
File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 94, in masked_arith_op
result[mask] = op(xrav[mask], yrav[mask])
TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1
the second image has values '-' in some columns. This will cast the entire column to a string and you won't be able to run arithmetic operations. This should do!
df = df.replace('-', 0)

Facing ValueError in max ()

I am troubling with the code section given below.. when I execute it, I get value error given below.
Segmask = result2
box=[]
[liver_res, num] = measure.label(result1, return_num=True)
region = measure.regionprops(liver_res)
for i in range(num):
box.append(region[i].area)
label_num = box.index(max(box)) + 1
liver_res[liver_res != label_num] = 0
liver_res[liver_res == label_num] = 1
Value error:
Traceback (most recent call last):
File "/content/gdrive/My Drive/LiTS/test.py", line 122, in <module>
predict(args)
File "/content/gdrive/My Drive/LiTS/test.py", line 91, in predict
label_num = box.index(max(box)) + 1
ValueError: max() arg is an empty sequence

Python 'int' object is not callable on For Loop

I know there's been quite a few of these but after looking through them, I wasn't able to solve my issue. I'm also still learning Python so a simple mix up could be happening.
Thus far the error is being reached on the line with the constructed for loop.
# down
column = 2
grid = [ 10, 10 ]
while range > 0:
# grab at the top
cur = genes[((grid[1] - 1) * grid[0]) + column]
prev = cur
for a in range((grid[1] - 2), -1, -1):
# start one below from the top
cur = genes[(a * grid[0]) + column]
genes[(a * grid[0]) + column] = prev
prev = cur
# now apply the change back to the top
genes[((grid[1] - 1) * grid[0]) + column] = prev
if get_fitness(genes, grid) > fitness:
print("After Down")
board = Board(genes, grid)
board.print()
print("-------------------")
return
range -= 1
As requested
Traceback (most recent call last):
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/tetris.py", line 144, in test_double_block
self.solveDouble([4, 4])
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/tetris.py", line 167, in solveDouble
best = genetic.get_best(fnGetFitness, None, optimalFitness, geneset,
fnDisplay, custom_mutate=fnCustomMutate, custom_create=fnCreate, maxAge=10)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/genetic.py", line 105, in get_best
maxAge, poolSize):
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/genetic.py", line 131, in _get_improvement
child = new_child(parent, pindex, parents)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/genetic.py", line 102, in fnNewChild
return fnMutate(parent)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/genetic.py", line 74, in fnMutate
return _mutate_custom(parent, custom_mutate, get_fitness)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/genetic.py", line 47, in _mutate_custom
custom_mutate(childGenes)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/tetris.py", line 159, in fnCustomMutate
mutate(genes, grid, window)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA
Projects/Tetris/tetris.py", line 65, in mutate
for a in range((grid[1] - 2), -1, -1):
TypeError: 'int' object is not callable
You defined range as a number
while range > 0:
...
range -= 1
But you later used it as a function
for a in range(...):
Rename the range integer to something else
For example, you could do a backward for loop
for r in range(top_range, 0, -1):
...

google places api in python is not working

Hello I am trying to use the google places api to get a list of all the McDonalds in Germany (weird exercise I know). This is my code (the box is smaller than the actual box for Germany so it does not take a long time to iterate):
import urllib2, simplejson
GOOGLE_API_KEY = 'my google api'
lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575
lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0
lat_incr = -.29
lng_incr = .29
while lng_curr<lng_SE:
lng_curr = lng_NW
while lat_curr > lng_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
url='https://maps.googleapis.com/maps/api/place/search/json?location=' + curr_location + '&sensor=false&key=' + GOOGLE_API_KEY + '&radius=20000&types=restaurant&name=mcdonalds'
response = urllib2.urlopen(url)
result = response.read()
d = simplejson.loads(result)
lng_curr += lng_incr
lat_curr += lat_incr
print (d)
And this is the output I got:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 404, in open
response = self._open(req, data)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 422, in _open
'_open', req)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "F:\WinPython-64bit-2.7.6.3\python-2.7.6.amd64\lib\urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] _ssl.c:507: EOF occurred in violation of protocol>
Any ideas on what am I doing wrong? Thanks!
My guess would be you've reached the usage limit.
If you try this, you'll see that your loop does never abort:
lat_NW = 53.4017872352
lng_NW = 5.954233125
lat_SE = 51.9766032969
lng_SE = 10.032130575
lat_curr = lat_NW
lng_curr = lng_NW
total_calls = 0
lat_incr = -.29
lng_incr = .29
while lng_curr<lng_SE:
lng_curr = lng_NW
while lat_curr > lng_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
print curr_location
lng_curr += lng_incr
lat_curr += lat_incr
What you probably want is something like this:
...
while lng_curr < lng_SE:
lat_curr = lat_NW
while lat_curr > lat_SE:
curr_location = str(lat_curr) + "," + str(lng_curr)
...
lat_curr += lat_incr
lng_curr += lng_incr

Categories

Resources