Could someone please help me with the below.
joinDf = join_df2(df_tgt_device_dim.withColumn("hashvalue", F.sha2(F.concat_ws(",", *valColumns), 256)).alias("target"),
df_final.withColumn("hashvalue", F.sha2(F.concat_ws(",", *valColumns), 256)).alias("source"),
conditions,
"full_outer",
keyColumns)
deltaDf = get_active_records(joinDf, common_cols, "Type2")
wind_spc = Window.partitionBy(*keyColumns).orderBy(col("effective_start_ts").desc())
df_device_new = deltaDf.withColumn("Rank", F.row_number().over(wind_spc))
deltaDf_final = df_device_new .filter( col("diff") != 'unchanged_act_records').withColumn("crnt_ind",when(df_device_new .Rank == 1 ,lit('Y'))\
.when(df_device_new.Rank != 1 ,lit('N'))).drop("Rank")
deltaDf_final.union(deltaDf.filter(col("diff") == 'unchanged_act_records').withColumn("crnt_ind",lit('N'))).createOrReplaceTempView(f"device_delta")
Below is the error.
NameError: name 'when' is not defined
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<command-104590> in <module>
17 wind_spc = Window.partitionBy(*keyColumns).orderBy(col("effective_start_ts").desc())
18 df_device_new = deltaDf.withColumn("Rank", F.row_number().over(wind_spc))
---> 19 deltaDf_final = df_device_new .filter( col("diff") != 'unchanged_act_records').withColumn("crnt_ind",when(df_device_new .Rank == 1 ,lit('Y'))\
.when(df_device_new.Rank != 1
21 deltaDf_final.union(deltaDf.filter(col("diff") == 'unchanged_act_records').withColumn("crnt_ind",lit('N'))).createOrReplaceTempView(f"device_delta")
NameError: name 'when' is not defined
I have tried F.when, but it did not work.
Could someone please assist thank you.
Try to use .otherwise instead of .when after first .when
df_device_new = deltaDf.withColumn("Rank", F.row_number().over(wind_spc))
deltaDf_final = df_device_new .filter( col("diff") != 'unchanged_act_records').withColumn("crnt_ind",when(df_device_new .Rank == 1 ,lit('Y'))\
.otherwise(df_device_new.Rank != 1 ,lit('N'))).drop("Rank")
Related
I'm using Colab to run the code but I get this error and I can't fix it. Could you help me out?
I don't know what to do in order to fix it because I have tried to change upper case or lower case.
#Inativos: ajustar nomes das colunas
dfInativos = dfInativos.rename(columns={'userId': 'id'})
dfInativos = dfInativos.rename(columns={'classId': 'ClasseId'})
dfInativos[['id','ClasseId','lastActivityDate','inactivityDaysCount','sevenDayInactiveStatus']] = dfInativos
#dfInativos['id'] = dfInativos['id'].astype(int, errors = 'ignore')
#dfInativos['ClasseId'] = dfInativos['ClasseId'].astype(int, errors = 'ignore')
dfInativos['id'] = pd.to_numeric(dfInativos['id'],errors = 'coerce')
dfInativos['ClasseId'] = pd.to_numeric(dfInativos['ClasseId'],errors = 'coerce')
#dfInativos.dropna(subset = ['lastActivityDate'], inplace=True)
dfInativos.drop_duplicates(subset = ['id','ClasseId'], inplace=True)
dfInativos['seven DayInactiveStatus'] = dfInativos['sevenDayInactiveStatus'].replace(0,'')
#Add Inactive data to main data frame
df = df.merge(dfInativos, on=['id','ClasseId'], how='left')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-79-10fe94c48d1f> in <module>()
2 dfInativos = dfInativos.rename(columns={'userId': 'id'})
3 dfInativos = dfInativos.rename(columns={'classId': 'ClasseId'})
----> 4 dfInativos[['id','ClasseId','lastActivityDate','inactivityDaysCount','sevenDayInactiveStatus']] = dfInativos
5
6
2 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexers.py in check_key_length(columns, key, value)
426 if columns.is_unique:
427 if len(value.columns) != len(key):
--> 428 raise ValueError("Columns must be same length as key")
429 else:
430 # Missing keys in columns are represented as -1
ValueError: Columns must be same length as key
I have a table in pyqt5 and when I modify a cell I call a method(changeIcon), inside this method I use an if statement to check which column has been changed and based on the column I change some items, everytime I modify the column 3 and 4 it returns me this error:
Traceback (most recent call last):
File "c:\FOLDER\App.py", line 291, in Change_method
RecursionError
This is the code :
def tabledata(self)
.....
self.tabella_registrazioni.itemChanged.connect(self.changeIcon)
.....
def changeIcon(self, item):
row = item.row()
col = item.column()
custcode = self.tabella_registrazioni.item(row, 3).text()
custname = self.tabella_registrazioni.item(row, 4).text()
if col == 3 :
if not custcode.isspace() and custcode != " " and custcode != "":
cname = d.execute("SELECT 1 FROM CODES WHERE ANCODICE = ?", (custcode)).fetchone()[0]
if cname != None:
self.tabella_registrazioni.setItem(row, 4, QtWidgets.QTableWidgetItem(str(cname)))
if col == 4 :
if not custname.isspace() and custname != " " and custname != "":
ccode = d.execute("SELECT 2 FROM CODES WHERE ANDESCRI = ?", (custname)).fetchone()[0]
if ccode != None:
self.tabella_registrazioni.setItem(row, 3, QtWidgets.QTableWidgetItem(str(ccode)))
I find out why it returned that error , everytime I change the item of the column 3 or 4 it gives a signal and calls this method, so when I try to change the column 4 automatically the column 3 changes based on column 4, but when the columns 3 changes the column 4 changes as well, this for infinte times. The solution was to block signals before setting the item and to enable them after I set the item :
self.tabella_registrazioni.blockSignals(True)
self.tabella_registrazioni.setItem(row, 3, QtWidgets.QTableWidgetItem(str(ccode)))
self.tabella_registrazioni.blockSignals(False)
I write this and i don't know why product_id_list is not defined if i have defined it like 4 lines before.
Any suggestions? I thin identation is alright so I don't have any more ideas and I also searched around without luck.
Thank you!!
def make_dataSet_rowWise(reorder_product):
print('unique Product in dataset = ', len(reorder_product.product_id.unique()))
print('unique order_id in dataset = ', len(reorder_product.order_id.unique()))
product_id_list = reorder_product.product_id.unique().tolist()
product_id_list.append("order_id")
product_id_dict = {}
i = 0
for prod_id in product_id_list:
product_id_dict[prod_id] = i
i = i+1
product_id_df = pd.Dataframe(columns = product_id_list)
row_list_all = []
order_id_list = reorder_product.order_id.unique()
i = 1
for id in order_id_list:
#print(i)
i = i+1
np_zeros = np.zeros(shape = [len(product_id_list)-1])
ordered_product_list = reorder_product.loc[reorder_product.order_id == id]["product_id"].tolist()
for order_prod in ordered_product_list:
np_zeros[product_id_dict.get(order_prod)] = 1
row_list = np_zeros.tolist()
row_list.append(id)
row_list_all.append(row_list)
return (row_list_all, product_id_list)
df_row_wise = make_dataSet_rowWise(reorder_product_99Pct)
product_id_df = pd.DataFrame(df_row_wise[0], columns = df_row_wise[1])
product_id_df.head()
The error I have is this one:
NameError Traceback (most recent call last)
<ipython-input-343-07bcac1b3b48> in <module>
7 i = 0
8
----> 9 for prod_id in product_id_list:
10 product_id_dict[prod_id] = i
11 i = i+1
NameError: name 'product_id_list' is not defined
As already mentioned by the other answers, your indentation is wrong.
My recommendation is that you use a IDE like VSCode, there is also a free web version https://vscode.dev/
With such kind of IDE you can see that your indentation is wrong, check screenshot and line 27
There are also wrong indentations with the 3 for loops. The correct indentation should be as the following
I think your indentation may be wrong, the for-loops and return statement is out of the function (with your indentation) so I indented it so that it would still be part of the function...
def make_dataSet_rowWise(reorder_product):
print('unique Product in dataset = ', len(reorder_product.product_id.unique()))
print('unique order_id in dataset = ', len(reorder_product.order_id.unique()))
product_id_list = reorder_product.product_id.unique().tolist()
product_id_list.append("order_id")
product_id_dict = {}
i = 0
for prod_id in product_id_list:
product_id_dict[prod_id] = i
i = i+1
product_id_df = pd.Dataframe(columns = product_id_list)
row_list_all = []
order_id_list = reorder_product.order_id.unique()
i = 1
for id in order_id_list:
#print(i)
i = i+1
np_zeros = id.zeros(shape = [len(product_id_list)-1])
ordered_product_list = reorder_product.loc[reorder_product.order_id == id]["product_id"].tolist()
for order_prod in ordered_product_list:
np_zeros[product_id_dict.get(order_prod)] = 1
row_list = np_zeros.tolist()
row_list.append(id)
row_list_all.append(row_list)
return (row_list_all, product_id_list)
I'm new here, but i think you either need to define the variable out of the scope of
def make_dataSet_rowWise(reorder_product):
OR indent the for loops to be inside
make_dataSet_rowWise
I am trying to write the constraint as shown in the image. But getting the below error:
> --------------------------------------------------------------------------- GurobiError Traceback (most recent call
> last) <ipython-input-112-d0e0b7b1cb5e> in <module>()
> ----> 1 Boiler_capacity = m.addConstrs((boiler_produced_thermal[t] <= boiler_thermal_max for t in time_slots), name = "Boiler_capacity")
>
> model.pxi in gurobipy.Model.addConstrs
> (../../src/python/gurobipy.c:89458)()
>
> model.pxi in gurobipy.Model.addConstr
> (../../src/python/gurobipy.c:87963)()
>
> linexpr.pxi in gurobipy.LinExpr.__sub__
> (../../src/python/gurobipy.c:34728)()
>
> linexpr.pxi in gurobipy.LinExpr.__add__
> (../../src/python/gurobipy.c:34333)()
>
> linexpr.pxi in gurobipy.LinExpr.add
> (../../src/python/gurobipy.c:31162)()
>
> GurobiError: Unsupported type (<class 'tuple'>) for LinExpr addition
> argument
What i have tried so far:
Boiler_capacity = m.addConstrs((boiler_produced_thermal[t] <= boiler_thermal_max for t in time_slots), name = "Boiler_capacity")
Where :
boiler_produced_thermal is variable with index time slot
boiler_thermal_max = 21000 is assigned to integer value.
time_slots = ['k1', 'k2','k3', 'k4', 'k5']
This code works:
Instead of using the variable name, if used the value assigned to a variable. It works. But i didn't get what is the actual reason behind this.
Boiler_capacity = m.addConstrs((boiler_produced_thermal[t] <= 21000 for t in time_slots), name = "Boiler_capacity")
Can somebody help me to understand the problem?.
How to debug "NameError: global name 'X' is not defined" in Python? I am pretty much new in Python. I am using jupyter_notebook with Python 2.7 to execute code. I am facing following error.
My code:
logFile = "NASAlog.txt"
def parseLogs():
parsed_logs=(sc
.textFile(logFile)
.map(parseApacheLogLine)
.cache())
access_logs = (parsed_logs
.filter(lambda s: s[1] == 1)
.map(lambda s: s[0])
.cache())
failed_logs = (parsed_logs
.filter(lambda s: s[1] == 0)
.map(lambda s: s[0]))
failed_logs_count = failed_logs.count()
if failed_logs_count > 0:
print 'Number of invalid logline: %d' % failed_logs.count()
for line in failed_logs.take(20):
print 'Invalid logline: %s' % line
print 'Read %d lines, successfully parsed %d lines, failed to parse %d lines' % (parsed_logs.count(), access_logs.count(), failed_logs.count())
return parsed_logs, access_logs, failed_logs
parsed_logs, access_logs, failed_logs = parseLogs()
ERROR
> NameError Traceback (most recent call last)
> <ipython-input-18-b365aa793252> in <module>()
> 24 return parsed_logs, access_logs, failed_logs
> 25
> ---> 26 parsed_logs, access_logs, failed_logs = parseLogs()
>
> <ipython-input-18-b365aa793252> in parseLogs()
> 2
> 3 def parseLogs():
> ----> 4 parsed_logs=(sc
> 5 .textFile(logFile)
> 6 .map(parseApacheLogLine)
>
> NameError: global name 'sc' is not defined
The problem is that you did never define sc. Therefore python can't find it. (Makes sense, doesn't it?)
Now there are several possible reasons:
- python is case-sensitive. Did you somewhere define SC instead of sc? ... Or Sc instead of sc?
You defined sc in another function (-> you defined it in a function outside parseLogs()). If you only define it there the variable will be local and just be available to the code inside the function. Add the line global sc to the first line of your function to make it accessible everywhere in you whole code.
You simply did not define sc.