How to convert for loop in list comprehension? - python

Trying to optimize my code I want to convert this for loop in a list comprehension, any help please?
fecha_añadir=pd.Timestamp('2200-01-01T12')
for x in range(0,len(df_vigencias)):
maximo=df_vigencias['max_vigencia'][x]
if df_vigencias['FECHA_FINAL_V'+str(int(maximo))][x] is pd.NaT:
df_vigencias['FECHA_FINAL_V'+str(int(maximo))][x]=fecha_añadir
I tried
[df_vigencias['FECHA_FINAL_V'+str(int(df_vigencias['max_vigencia']))]=fecha_añadir if df_vigencias['FECHA_FINAL_V'+str(int(df_vigencias['max_vigencia']))] is pd.Nat else df_vigencias['FECHA_FINAL_V'+str(int(df_vigencias['max_vigencia']))] for x in range(0,len(df_vigencias))]
This is the data frame
First I want to find the number in the last column then I use that number to look for the column name where I need to insert a value,
I though a listh comprehension will make my code faster, but any other solution could work

Please have a look at this tutorial:
How to Convert Loops to List Comprehensions in Python

Related

python list comprehension with if condition looping

Is it possible to use list comprehension for a dataframe if I want to change one column's value based on the condition of another column's value.
The code I'm hoping to make work would be something like this:
return ['lower_level' for x in usage_time_df['anomaly'] if [y < lower_outlier for y in usage_time_df['device_years']]
Thanks!
I don't think what you want to do can be done in a list comprehension, and if it can, it will definitely not be efficient.
Assuming a dataframe usage_time_df with two columns, anomaly and device_years, if I understand correctly, you want to set the value in anomaly to lower_level when the value in device_years does not reach lower_outlier (which I guess is a float). The natural way to do that is:
usage_time_df.loc[usage_time_df['device_years'] < lower_outlier, 'anomaly'] = 'lower_level'

How to create new array deducting segments of existing array

I am trying to create new array out of an existing array in Python.
I read some of already existing and similar questions but I still can not solve the problem.
For example:
I have array A = [4,6,9,15] and I want to create B =[(6-4),(9-6),(15-9)].
I tried to do it in for loop like this:
deltaB=[]
for i in range(0,len(A)):
deltaB[i]=A[i]-A[i-1]
deltaB.append(deltaB[i])
But that does not work... probably because I am writing code completely wrong since I'm new in Python and programming in general.
Can you help and write me code for this?
Many thanks upfront
List comprehension
Probably the best way to do this is using list comprehension:
[xj-xi for xi,xj in zip(A,A[1:])]
which generates:
>>> [xj-xi for xi,xj in zip(A,A[1:])]
[2, 3, 6]
Here we first zip(..) A (the list) and A[1:] the slice of the list that omits the first element together into tuples. For each such tuple (xi,xj) we add xj-xi to the list.
The error
The error occurs because in the for loop, you start from 0 and stop before len(A), it should be starting from 1 and stop before len(A). Furthermore you cannot first assign to an index that does not exist, you need to directly append it:
deltaB=[]
for i in range(1,len(A)):
deltaB.append(A[i]-A[i-1])

Tuple specific extraction with for loop due to large quantity of lists

I would like to extract more than 1 pair of tuples in even or odd positions. For example the first and the last. I have read many questions and all of them are referring to how to extract a specific one from each tuple, but I have to have 2 pairs.
I am using for example test2 (already made as tuple) and currently trying to figure it out with:
Tuplewanted=[x[0::1] for x in Tuple]
Tuplewanted
Out[44]:
[(778933.8147968281, 5803816.850292235),
(778999.2820487045, 5804014.491034968),
(779011.4321377204, 5804048.532974694),
(779024.8198435705, 5804081.474176192),
(779039.3061023126, 5804115.648560766),
(779055.1628175485, 5804146.376816435),
(779072.6698779828, 5804178.971719031),
(779121.9406760866, 5804267.038294602)]
while I want only the 1st tuple and the last
Tuplewanted= [(778933.8147968281, 5803816.850292235),(779121.9406760866, 5804267.038294602)]
Anyone?
Note that Typewanted[0] and Typewanted[7] and the .append cannot work since I have a list with more than 100000 of those tuple lists.
Solution
Tuplewanted['coords']=[ix.coords[::len(ix.coords)-1]for ix in Tuple.geometry]
Thank you again
I am not quite sure what you want exactly. If you just want the last tuple, you can do this :list[-1]

Python Pandas replace string based on format

Please, is there any ways to replace "x-y" by "x,x+1,x+2,...,y" in every row in a data frame? (Where x, y are integer).
For example, I want to replace every row like this:
"1-3,7" by "1,2,3,7"
"1,4,6-9,11-13,5" by "1,4,6,7,8,9,11,12,13,5"
etc
I know that by looping through lines and using regular expression we can do that. But the table is quite big and it takes quite some time. so I think using pandas might be faster.
Thanks alot
In pandas you can use apply to apply any function to either rows or columns in a DataFrame. The function can be passed with a lambda, or defined separately.
(side-remark: your example does not entirely make clear if you actually have a 2-D DataFrame or just a 1-D Series. Either way, apply can be used)
The next step is to find the right function. Here's a rough version (without regular expressions):
def make_list(str):
lst = str.split(',')
newlst = []
for i in lst:
if "-" in i:
newlst.extend(range(*[int(j) for j in i.split("-")]))
else:
newlst.append(int(i))
return newlst

Quick checkup on python list comprehension

I currently generate lists using following expression (T and no_jobs are integers):
for i in xrange(no_jobs):
row = row + T * [i]
The first thing I came up with for converting it into a list comprehension statement was:
[T*[i] for i in xrange(no_jobs)]
But this obviously creates a nested list which is not what I'm looking for. All my other ideas seems a litle clunky so if anyone has a pythonic and elegant way of creating these types of lists I would be gratefull.
Nested loops.
[i for i in xrange(no_jobs) for x in xrange(T)]
But this obviously creates a nested list which is not what I'm looking for.
So just flatten the result. List addition is concatenation, so we can put all the lists together by 'summing' them (with an empty list as an "accumulator").
sum((T*[i] for i in xrange(no_jobs)), [])

Categories