Is there a limit for zip() elements in loops in python?
up vote
0
down vote
favorite
Something weird happened to me today. I needed to create a list based on a sequence of if statements. My dataframe looks something like this:
prom_lect4b_rbd prom_lect2m_rbd prom_lect8b_rbd prom_lect6b_rbd
100 np.nan 80 200
np.nan np.nan 40 1000
np.nan np.nan np.nan 90
230 100 80 100
Columns are orderer according to their priority. The list (or column) I'm trying to create takes the first value from those rows that is not nan
. So, in this case I want a column that looks like this:
simce_final_lect
100
40
90
230
I tried the following:
cols=[simces.prom_lect4b_rbd, simces.prom_lect2m_rbd, simces.prom_lect8b_rbd, simces.prom_lect6b_rbd]
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False else m if np.isnan(m)==False
else np.nan for j,k,l,m in zip(cols[0],cols[1],cols[2],cols[3])]
And that just copies two values (out of 8752) to the list. But if I limit my zip to just j,k,l
, it works perfectly:
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False
else np.nan for j,k,l in zip(cols[0],cols[1],cols[2])]
Do you know what is happening? Else, is there a more efficient solution to my problem?
python pandas for-loop list-comprehension
add a comment |
up vote
0
down vote
favorite
Something weird happened to me today. I needed to create a list based on a sequence of if statements. My dataframe looks something like this:
prom_lect4b_rbd prom_lect2m_rbd prom_lect8b_rbd prom_lect6b_rbd
100 np.nan 80 200
np.nan np.nan 40 1000
np.nan np.nan np.nan 90
230 100 80 100
Columns are orderer according to their priority. The list (or column) I'm trying to create takes the first value from those rows that is not nan
. So, in this case I want a column that looks like this:
simce_final_lect
100
40
90
230
I tried the following:
cols=[simces.prom_lect4b_rbd, simces.prom_lect2m_rbd, simces.prom_lect8b_rbd, simces.prom_lect6b_rbd]
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False else m if np.isnan(m)==False
else np.nan for j,k,l,m in zip(cols[0],cols[1],cols[2],cols[3])]
And that just copies two values (out of 8752) to the list. But if I limit my zip to just j,k,l
, it works perfectly:
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False
else np.nan for j,k,l in zip(cols[0],cols[1],cols[2])]
Do you know what is happening? Else, is there a more efficient solution to my problem?
python pandas for-loop list-comprehension
2
Your last column is shorter than the others.zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function:first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
thenresult = list(map(first_not_nan, zip(A,B,C,D)))
.
– Bakuriu
Nov 8 at 19:25
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of onlynan
? How could I fill it with a nan in that case? Thanks
– Juan C
Nov 8 at 19:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Something weird happened to me today. I needed to create a list based on a sequence of if statements. My dataframe looks something like this:
prom_lect4b_rbd prom_lect2m_rbd prom_lect8b_rbd prom_lect6b_rbd
100 np.nan 80 200
np.nan np.nan 40 1000
np.nan np.nan np.nan 90
230 100 80 100
Columns are orderer according to their priority. The list (or column) I'm trying to create takes the first value from those rows that is not nan
. So, in this case I want a column that looks like this:
simce_final_lect
100
40
90
230
I tried the following:
cols=[simces.prom_lect4b_rbd, simces.prom_lect2m_rbd, simces.prom_lect8b_rbd, simces.prom_lect6b_rbd]
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False else m if np.isnan(m)==False
else np.nan for j,k,l,m in zip(cols[0],cols[1],cols[2],cols[3])]
And that just copies two values (out of 8752) to the list. But if I limit my zip to just j,k,l
, it works perfectly:
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False
else np.nan for j,k,l in zip(cols[0],cols[1],cols[2])]
Do you know what is happening? Else, is there a more efficient solution to my problem?
python pandas for-loop list-comprehension
Something weird happened to me today. I needed to create a list based on a sequence of if statements. My dataframe looks something like this:
prom_lect4b_rbd prom_lect2m_rbd prom_lect8b_rbd prom_lect6b_rbd
100 np.nan 80 200
np.nan np.nan 40 1000
np.nan np.nan np.nan 90
230 100 80 100
Columns are orderer according to their priority. The list (or column) I'm trying to create takes the first value from those rows that is not nan
. So, in this case I want a column that looks like this:
simce_final_lect
100
40
90
230
I tried the following:
cols=[simces.prom_lect4b_rbd, simces.prom_lect2m_rbd, simces.prom_lect8b_rbd, simces.prom_lect6b_rbd]
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False else m if np.isnan(m)==False
else np.nan for j,k,l,m in zip(cols[0],cols[1],cols[2],cols[3])]
And that just copies two values (out of 8752) to the list. But if I limit my zip to just j,k,l
, it works perfectly:
simce_final_lect=[j if np.isnan(j)==False else k if np.isnan(k)==False
else l if np.isnan(l)==False
else np.nan for j,k,l in zip(cols[0],cols[1],cols[2])]
Do you know what is happening? Else, is there a more efficient solution to my problem?
python pandas for-loop list-comprehension
python pandas for-loop list-comprehension
asked Nov 8 at 19:19
Juan C
19311
19311
2
Your last column is shorter than the others.zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function:first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
thenresult = list(map(first_not_nan, zip(A,B,C,D)))
.
– Bakuriu
Nov 8 at 19:25
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of onlynan
? How could I fill it with a nan in that case? Thanks
– Juan C
Nov 8 at 19:35
add a comment |
2
Your last column is shorter than the others.zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function:first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
thenresult = list(map(first_not_nan, zip(A,B,C,D)))
.
– Bakuriu
Nov 8 at 19:25
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of onlynan
? How could I fill it with a nan in that case? Thanks
– Juan C
Nov 8 at 19:35
2
2
Your last column is shorter than the others.
zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function: first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
then result = list(map(first_not_nan, zip(A,B,C,D)))
.– Bakuriu
Nov 8 at 19:25
Your last column is shorter than the others.
zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function: first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
then result = list(map(first_not_nan, zip(A,B,C,D)))
.– Bakuriu
Nov 8 at 19:25
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of only
nan
? How could I fill it with a nan in that case? Thanks– Juan C
Nov 8 at 19:35
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of only
nan
? How could I fill it with a nan in that case? Thanks– Juan C
Nov 8 at 19:35
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can use bfill(axis=1)
and select the first col.
df.bfill(axis=1).iloc[:,0]
0 100.0
1 40.0
2 90.0
3 230.0
Name: prom_lect4b_rbd, dtype: float64
## For list
df.bfill(axis=1).iloc[:,0].tolist()
['100', '40', 90, '230']
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
add a comment |
up vote
0
down vote
Use first_valid_index()
:
df.apply(lambda x: x[x.first_valid_index()], axis=1)
Yields:
0 100.0
1 40.0
2 90.0
3 230.0
dtype: float64
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use bfill(axis=1)
and select the first col.
df.bfill(axis=1).iloc[:,0]
0 100.0
1 40.0
2 90.0
3 230.0
Name: prom_lect4b_rbd, dtype: float64
## For list
df.bfill(axis=1).iloc[:,0].tolist()
['100', '40', 90, '230']
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
add a comment |
up vote
2
down vote
accepted
You can use bfill(axis=1)
and select the first col.
df.bfill(axis=1).iloc[:,0]
0 100.0
1 40.0
2 90.0
3 230.0
Name: prom_lect4b_rbd, dtype: float64
## For list
df.bfill(axis=1).iloc[:,0].tolist()
['100', '40', 90, '230']
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use bfill(axis=1)
and select the first col.
df.bfill(axis=1).iloc[:,0]
0 100.0
1 40.0
2 90.0
3 230.0
Name: prom_lect4b_rbd, dtype: float64
## For list
df.bfill(axis=1).iloc[:,0].tolist()
['100', '40', 90, '230']
You can use bfill(axis=1)
and select the first col.
df.bfill(axis=1).iloc[:,0]
0 100.0
1 40.0
2 90.0
3 230.0
Name: prom_lect4b_rbd, dtype: float64
## For list
df.bfill(axis=1).iloc[:,0].tolist()
['100', '40', 90, '230']
edited Nov 8 at 19:42
answered Nov 8 at 19:31
Abhi
2,086319
2,086319
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
add a comment |
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
1
1
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
Thank you, worked like a charm! Pythonic and simple, way less convoluted than my solution.
– Juan C
Nov 8 at 19:39
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
@JuanC Happy to help. :)
– Abhi
Nov 8 at 19:42
add a comment |
up vote
0
down vote
Use first_valid_index()
:
df.apply(lambda x: x[x.first_valid_index()], axis=1)
Yields:
0 100.0
1 40.0
2 90.0
3 230.0
dtype: float64
add a comment |
up vote
0
down vote
Use first_valid_index()
:
df.apply(lambda x: x[x.first_valid_index()], axis=1)
Yields:
0 100.0
1 40.0
2 90.0
3 230.0
dtype: float64
add a comment |
up vote
0
down vote
up vote
0
down vote
Use first_valid_index()
:
df.apply(lambda x: x[x.first_valid_index()], axis=1)
Yields:
0 100.0
1 40.0
2 90.0
3 230.0
dtype: float64
Use first_valid_index()
:
df.apply(lambda x: x[x.first_valid_index()], axis=1)
Yields:
0 100.0
1 40.0
2 90.0
3 230.0
dtype: float64
answered Nov 8 at 19:29
rahlf23
4,3501629
4,3501629
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214723%2fis-there-a-limit-for-zip-elements-in-loops-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Your last column is shorter than the others.
zip
stops when the shorter list ends. BTW: you should really consider extracting the expression that takes the value into a function:first_not_nan = lambda vals: next(x for x in vals if not math.isnan(x))
thenresult = list(map(first_not_nan, zip(A,B,C,D)))
.– Bakuriu
Nov 8 at 19:25
Thanks Bakuriu! Could you explain me a little further your approach? I'm not really used to lambda expressions outside the apply function when trying to do an operation in every row of a Series. When I used both commands it got me a list of just 148 values. Maybe it stopped when it found a row consisting of only
nan
? How could I fill it with a nan in that case? Thanks– Juan C
Nov 8 at 19:35