I am trying to put this conditional clause into my list comp "arr".
arr = [[(i*n+j) for i in range(n)] for j in range(n)]
with
if ( 2*i<=j+i+1<=n+1 and i>0)
as the conditional. I tried putting this:
-with a else statement in a ternary at the front: "" , yet this produces unwanted elements in the array.
-as is after my for statements (i, j, and even tried both)
Any Ideas on how to get this to compute without adding to much complexity?
Desired Example Ouput:
from pandas import DataFrame as df
n = 5
arr= [NEEDS HELP HERE]
print(df(arr))
>>>
0 1 2 3 4
0 NaN 5 NaN NaN NaN
1 NaN 6 11.0 NaN NaN
2 NaN 7 12.0 17.0 NaN
3 NaN 8 13.0 NaN NaN
4 NaN 9 NaN NaN NaN
FOR n = 3
>>>
0 1 2
0 NaN 3 NaN
1 NaN 4 7.0
2 NaN 5 NaN
For n = 2
>>>
0 1
0 NaN 2
1 NaN 3
For n = 10 (my code can generate the below)
>>>
0 1 2 3 4 5 6 7 8 9
0 _ 10 _ _ _ _ _ _ _ _
1 _ 11 21 _ _ _ _ _ _ _
2 _ 12 22 32 _ _ _ _ _ _
3 _ 13 23 33 43 _ _ _ _ _
4 _ 14 24 34 44 54 _ _ _ _
5 _ 15 25 35 45 55 _ _ _ _
6 _ 16 26 36 46 _ _ _ _ _
7 _ 17 27 37 _ _ _ _ _ _
8 _ 18 28 _ _ _ _ _ _ _
9 _ 19 _ _ _ _ _ _ _ _
As you can see each should produce a nxn matrix. (I can replace each "_" with np.NaN)
I will post the solution below. Thanks So much for all the contributions.
You can put it in the inner list comprehension:
arr = [[(i*n+j) for i in range(n) if ( 2*i<=j+i+1<=n+1 and i>0)] for j in range(n)]
Output:
n = 10 # for example
print(arr)
[[10],
[11, 21],
[12, 22, 32],
[13, 23, 33, 43],
[14, 24, 34, 44, 54],
[15, 25, 35, 45, 55],
[16, 26, 36, 46],
[17, 27, 37],
[18, 28],
[19]]
EDIT:
If you want it in a DataFrame with your desired output:
import numpy as np
import pandas as pd
n = 10
arr = [[np.NaN] + [(i*n+j) for i in range(n) if ( 2*i<=j+i+1<=n+1 and i>0)] + [np.NaN] * (n - j - 2) for j in range(n)]
pd.DataFrame(arr)
0 1 2 3 4 5 6 7 8 9
0 NaN 10 NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN 11 21.0 NaN NaN NaN NaN NaN NaN NaN
2 NaN 12 22.0 32.0 NaN NaN NaN NaN NaN NaN
3 NaN 13 23.0 33.0 43.0 NaN NaN NaN NaN NaN
4 NaN 14 24.0 34.0 44.0 54.0 NaN NaN NaN NaN
5 NaN 15 25.0 35.0 45.0 55.0 NaN NaN NaN NaN
6 NaN 16 26.0 36.0 46.0 NaN NaN NaN NaN NaN
7 NaN 17 27.0 37.0 NaN NaN NaN NaN NaN NaN
8 NaN 18 28.0 NaN NaN NaN NaN NaN NaN NaN
9 NaN 19 NaN NaN NaN NaN NaN NaN NaN NaN
You can simplify things a little as @user2357112 suggested:
import numpy as np
import pandas as pd
n = 6
arr = ([i*n+j for i in range(1,n-j+1) if i<=j+1] for j in range(n))
df = pd.DataFrame([np.NaN]+x+[np.NaN]*(n-len(x)-1) for x in arr)
print(df)
Output:
0 1 2 3 4 5
0 NaN 6 NaN NaN NaN NaN
1 NaN 7 13.0 NaN NaN NaN
2 NaN 8 14.0 20.0 NaN NaN
3 NaN 9 15.0 21.0 NaN NaN
4 NaN 10 16.0 NaN NaN NaN
5 NaN 11 NaN NaN NaN NaN
from pandas import DataFrame as df
import numpy as np
n = {USERINPUT_Var_(int>1)}
arr = [[i*n+j if ( 2*i<=j+i+1<=n+1 and i>0) else np.NaN for i in range(n)] for j in range(n)]
print(df(arr))
I could still use some help simplifing the conditional now.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.