Delimited columns on console tabular prints - scripting

I have a tabular text printed to the console, like below (output of the nmcli dev wifi command):
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
* CAYO Infra 5 54 Mbit/s 89 ▂▄▆█ WPA2
Jean Infra 1 54 Mbit/s 72 ▂▄▆_ WPA2
Jackson Infra 1 54 Mbit/s 52 ▂▄__ WPA2
capitao Infra 6 54 Mbit/s 52 ▂▄__ WPA1 WPA2
Fernanda Infra 11 54 Mbit/s 49 ▂▄__ WPA1 WPA2
Morais-Room Infra 11 54 Mbit/s 49 ▂▄__ WPA1 WPA2
Rinaldi Infra 3 54 Mbit/s 45 ▂▄__ WPA2
Eni Infra 1 54 Mbit/s 34 ▂▄__ WPA1 WPA2
Doidinho Infra 6 54 Mbit/s 34 ▂▄__ WPA1 WPA2
Casa das Primas Infra 8 54 Mbit/s 32 ▂▄__ WPA2
Marcos Vinicius Infra 3 54 Mbit/s 30 ▂___ WPA1 WPA2
MONICA Infra 11 54 Mbit/s 30 ▂___ WPA2
gabriela Infra 3 54 Mbit/s 29 ▂___ WPA1 WPA2
Rinaldi Infra 3 54 Mbit/s 25 ▂___ WPA1 WPA2
LUAN Infra 6 54 Mbit/s 25 ▂___ WPA2
Vanessa Infra 11 54 Mbit/s 25 ▂___ WPA2
2 M Constru Infra 11 54 Mbit/s 22 ▂___ WPA2
I need to separate each column (and yours respective rows) by some char delimiter (like commas, for example).
How can I do this?

Related

Dataframe Operation Splicing

I have a single column dataframe without headers and I want to split it into multiple columns as follows
The current dataframe -
1
2
3
4
5
.
.
100
I want to represent it as -
1 6 .. .. 96
2 7 .. .. 97
3 8 .. .. 98
4 9 .. .. 99
5 10 .. .. 100
Assuming such a DataFrame:
df = pd.DataFrame({'col': range(1, 101)})
you can use the underlying numpy array to reshape:
df2 = pd.DataFrame(df['col'].to_numpy().reshape(5, -1, order='F'))
output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 \
0 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91
1 2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87 92
2 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93
3 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94
4 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
19
0 96
1 97
2 98
3 99
4 100

Optimize Oracle Query - for View

I have single question, to optimize a View Query using Oracle. Is there anyone able to help me out.
I'm about to select circuitname and oldname (the old name of circuitname) from only one table. But each circuitname, possibly has more than 1 sub name (oldname, sub-oldname, etc), and i want to join all of them with this View Query. This query is good, but when I select 1 by 1 circuitname, oldname, sub-oldname, etc until 20 circuitname, it takes too much time. I want to optimize the query, to perform less time, before the app timeout.
Here's the query
CREATE OR REPLACE FORCE EDITIONABLE VIEW "IMS"."VW_SI_CASCADE" ("R1", "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10", "L11", "L12", "L13", "L14", "L15", "L16", "L17", "L18", "L19", "L20", "L21", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10", "M11", "M12", "M13", "M14", "M15", "M16", "M17", "M18", "M19", "M20", "M21") AS
SELECT
ROWNUM AS R1,
V1.OLDNAME AS L1,
V1.CIRCUITNAME AS L2,
V2.CIRCUITNAME AS L3,
V3.CIRCUITNAME AS L4,
V4.CIRCUITNAME AS L5,
V5.CIRCUITNAME AS L6,
V6.CIRCUITNAME AS L7,
V7.CIRCUITNAME AS L8,
V8.CIRCUITNAME AS L9,
V9.CIRCUITNAME AS L10,
V10.CIRCUITNAME AS L11,
V11.CIRCUITNAME AS L12,
V12.CIRCUITNAME AS L13,
V13.CIRCUITNAME AS L14,
V14.CIRCUITNAME AS L15,
V15.CIRCUITNAME AS L16,
V16.CIRCUITNAME AS L17,
V17.CIRCUITNAME AS L18,
V18.CIRCUITNAME AS L19,
V19.CIRCUITNAME AS L20,
V20.CIRCUITNAME AS L21,
V1.OLDCIRCUITID AS M1,
V1.CIRCUITID AS M2,
V2.CIRCUITID AS M3,
V3.CIRCUITID AS M4,
V4.CIRCUITID AS M5,
V5.CIRCUITID AS M6,
V6.CIRCUITID AS M7,
V7.CIRCUITID AS M8,
V8.CIRCUITID AS M9,
V9.CIRCUITID AS M10,
V10.CIRCUITID AS M11,
V11.CIRCUITID AS M12,
V12.CIRCUITID AS M13,
V13.CIRCUITID AS M14,
V14.CIRCUITID AS M15,
V15.CIRCUITID AS M16,
V16.CIRCUITID AS M17,
V17.CIRCUITID AS M18,
V18.CIRCUITID AS M19,
V19.CIRCUITID AS M20,
V20.CIRCUITID AS M21
FROM VW_SI_OLDNAME V1
LEFT JOIN VW_SI_OLDNAME V2 ON V2.OLDNAME = V1.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V3 ON V3.OLDNAME = V2.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V4 ON V4.OLDNAME = V3.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V5 ON V5.OLDNAME = V4.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V6 ON V6.OLDNAME = V5.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V7 ON V7.OLDNAME = V6.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V8 ON V8.OLDNAME = V7.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V9 ON V9.OLDNAME = V8.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V10 ON V10.OLDNAME = V9.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V11 ON V11.OLDNAME = V10.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V12 ON V12.OLDNAME = V11.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V13 ON V13.OLDNAME = V12.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V14 ON V14.OLDNAME = V13.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V15 ON V15.OLDNAME = V14.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V16 ON V16.OLDNAME = V15.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V17 ON V17.OLDNAME = V16.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V18 ON V18.OLDNAME = V17.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V19 ON V19.OLDNAME = V18.CIRCUITNAME
LEFT JOIN VW_SI_OLDNAME V20 ON V20.OLDNAME = V19.CIRCUITNAME;
Pardon if I have any mistakes, and kindly need your help guys, my fellow heroes!
You're querying the view twenty times. That's going to be slow. You could try using subquery factoring, so you query the view once and re-use the result set multiple times.
CREATE OR REPLACE FORCE EDITIONABLE VIEW "IMS"."VW_SI_CASCADE" ("R1", "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10", "L11", "L12", "L13", "L14", "L15", "L16", "L17", "L18", "L19", "L20", "L21", "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10", "M11", "M12", "M13", "M14", "M15", "M16", "M17", "M18", "M19", "M20", "M21") AS
with cte as (
select * from VW_SI_OLDNAME
)
SELECT
ROWNUM AS R1,
V1.OLDNAME AS L1,
V1.CIRCUITNAME AS L2,
V2.CIRCUITNAME AS L3,
V3.CIRCUITNAME AS L4,
V4.CIRCUITNAME AS L5,
V5.CIRCUITNAME AS L6,
V6.CIRCUITNAME AS L7,
V7.CIRCUITNAME AS L8,
V8.CIRCUITNAME AS L9,
V9.CIRCUITNAME AS L10,
V10.CIRCUITNAME AS L11,
V11.CIRCUITNAME AS L12,
V12.CIRCUITNAME AS L13,
V13.CIRCUITNAME AS L14,
V14.CIRCUITNAME AS L15,
V15.CIRCUITNAME AS L16,
V16.CIRCUITNAME AS L17,
V17.CIRCUITNAME AS L18,
V18.CIRCUITNAME AS L19,
V19.CIRCUITNAME AS L20,
V20.CIRCUITNAME AS L21,
V1.OLDCIRCUITID AS M1,
V1.CIRCUITID AS M2,
V2.CIRCUITID AS M3,
V3.CIRCUITID AS M4,
V4.CIRCUITID AS M5,
V5.CIRCUITID AS M6,
V6.CIRCUITID AS M7,
V7.CIRCUITID AS M8,
V8.CIRCUITID AS M9,
V9.CIRCUITID AS M10,
V10.CIRCUITID AS M11,
V11.CIRCUITID AS M12,
V12.CIRCUITID AS M13,
V13.CIRCUITID AS M14,
V14.CIRCUITID AS M15,
V15.CIRCUITID AS M16,
V16.CIRCUITID AS M17,
V17.CIRCUITID AS M18,
V18.CIRCUITID AS M19,
V19.CIRCUITID AS M20,
V20.CIRCUITID AS M21
FROM cte V1
LEFT JOIN cte V2 ON V2.OLDNAME = V1.CIRCUITNAME
LEFT JOIN cte V3 ON V3.OLDNAME = V2.CIRCUITNAME
LEFT JOIN cte V4 ON V4.OLDNAME = V3.CIRCUITNAME
LEFT JOIN cte V5 ON V5.OLDNAME = V4.CIRCUITNAME
LEFT JOIN cte V6 ON V6.OLDNAME = V5.CIRCUITNAME
LEFT JOIN cte V7 ON V7.OLDNAME = V6.CIRCUITNAME
LEFT JOIN cte V8 ON V8.OLDNAME = V7.CIRCUITNAME
LEFT JOIN cte V9 ON V9.OLDNAME = V8.CIRCUITNAME
LEFT JOIN cte V10 ON V10.OLDNAME = V9.CIRCUITNAME
LEFT JOIN cte V11 ON V11.OLDNAME = V10.CIRCUITNAME
LEFT JOIN cte V12 ON V12.OLDNAME = V11.CIRCUITNAME
LEFT JOIN cte V13 ON V13.OLDNAME = V12.CIRCUITNAME
LEFT JOIN cte V14 ON V14.OLDNAME = V13.CIRCUITNAME
LEFT JOIN cte V15 ON V15.OLDNAME = V14.CIRCUITNAME
LEFT JOIN cte V16 ON V16.OLDNAME = V15.CIRCUITNAME
LEFT JOIN cte V17 ON V17.OLDNAME = V16.CIRCUITNAME
LEFT JOIN cte V18 ON V18.OLDNAME = V17.CIRCUITNAME
LEFT JOIN cte V19 ON V19.OLDNAME = V18.CIRCUITNAME
LEFT JOIN cte V20 ON V20.OLDNAME = V19.CIRCUITNAME;
Beyond that you may need to consider the internal logic of the view. There may be a better way to re-construct the chain of names. You also might be exposed to cycles in the re-naming.
For example, your VW_SI_OLDNAME contains a data like this:
with VW_SI_OLDNAME as (
select
'name_'||to_char(level-1,'fm0000') OLDNAME
,'name_'||to_char(level ,'fm0000') CIRCUITNAME
,level-1 OLDCIRCUITID
,level CIRCUITID
from dual
connect by level<=100
)
select *
from VW_SI_OLDNAME;
Example:
OLDNAME CIRCUITNAM OLDCIRCUITID CIRCUITID
---------- ---------- ------------ ----------
name_0000 name_0001 0 1
name_0001 name_0002 1 2
name_0002 name_0003 2 3
name_0003 name_0004 3 4
name_0004 name_0005 4 5
...
name_0028 name_0029 28 29
name_0029 name_0030 29 30
name_0030 name_0031 30 31
...
name_0097 name_0098 97 98
name_0098 name_0099 98 99
name_0099 name_0100 99 100
In this case, you can easily create your view using MATCH_RECOGNIZE that will scan your VW_SI_OLDNAME just once:
VW_SI_CASCADE as (
select *
from VW_SI_OLDNAME
match_recognize(
MEASURES
classifier() event
,match_number() match
,prev(CIRCUITID,1) CIRCUITID_1
,prev(CIRCUITID,2) CIRCUITID_2
,prev(CIRCUITID,3) CIRCUITID_3
,prev(CIRCUITID,4) CIRCUITID_4
,prev(CIRCUITID,5) CIRCUITID_5
,prev(CIRCUITID,6) CIRCUITID_6
,prev(CIRCUITID,7) CIRCUITID_7
,prev(CIRCUITID,8) CIRCUITID_8
,prev(CIRCUITNAME,1) CIRCUITNAME_1
,prev(CIRCUITNAME,2) CIRCUITNAME_2
,prev(CIRCUITNAME,3) CIRCUITNAME_3
,prev(CIRCUITNAME,4) CIRCUITNAME_4
,prev(CIRCUITNAME,5) CIRCUITNAME_5
,prev(CIRCUITNAME,6) CIRCUITNAME_6
,prev(CIRCUITNAME,7) CIRCUITNAME_7
,prev(CIRCUITNAME,8) CIRCUITNAME_8
ALL ROWS PER MATCH
PATTERN (p)
DEFINE
p AS OLDNAME = prev(CIRCUITNAME)
)
)
I've created only 1-8 just to make this example shorter.
Full example with test data:
with
VW_SI_OLDNAME as (
select
'name_'||to_char(level-1,'fm0000') OLDNAME
,'name_'||to_char(level ,'fm0000') CIRCUITNAME
,level-1 OLDCIRCUITID
,level CIRCUITID
from dual
connect by level<=100
)
,VW_SI_CASCADE as (
select *
from VW_SI_OLDNAME
match_recognize(
MEASURES
classifier() event
,match_number() match
,prev(CIRCUITID,1) CIRCUITID_1
,prev(CIRCUITID,2) CIRCUITID_2
,prev(CIRCUITID,3) CIRCUITID_3
,prev(CIRCUITID,4) CIRCUITID_4
,prev(CIRCUITID,5) CIRCUITID_5
,prev(CIRCUITID,6) CIRCUITID_6
,prev(CIRCUITID,7) CIRCUITID_7
,prev(CIRCUITID,8) CIRCUITID_8
,prev(CIRCUITNAME,1) CIRCUITNAME_1
,prev(CIRCUITNAME,2) CIRCUITNAME_2
,prev(CIRCUITNAME,3) CIRCUITNAME_3
,prev(CIRCUITNAME,4) CIRCUITNAME_4
,prev(CIRCUITNAME,5) CIRCUITNAME_5
,prev(CIRCUITNAME,6) CIRCUITNAME_6
,prev(CIRCUITNAME,7) CIRCUITNAME_7
,prev(CIRCUITNAME,8) CIRCUITNAME_8
ALL ROWS PER MATCH
PATTERN (p)
DEFINE
p AS OLDNAME = prev(CIRCUITNAME)
)
)
select *
from VW_SI_CASCADE;
Results:
EVENT MATCH CIRCUITID_1 CIRCUITID_2 CIRCUITID_3 CIRCUITID_4 CIRCUITID_5 CIRCUITID_6 CIRCUITID_7 CIRCUITID_8 CIRCUITNAM CIRCUITNAM CIRCUITNAM CIRCUITNAM CIRCUITNAM CIRCUITNAM CIRCUITNAM CIRCUITNAM OLDNAME CIRCUITNAM OLDCIRCUITID CIRCUITID
----- ------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------ ----------
P 1 1 name_0001 name_0001 name_0002 1 2
P 2 2 1 name_0002 name_0001 name_0002 name_0003 2 3
P 3 3 2 1 name_0003 name_0002 name_0001 name_0003 name_0004 3 4
P 4 4 3 2 1 name_0004 name_0003 name_0002 name_0001 name_0004 name_0005 4 5
P 5 5 4 3 2 1 name_0005 name_0004 name_0003 name_0002 name_0001 name_0005 name_0006 5 6
P 6 6 5 4 3 2 1 name_0006 name_0005 name_0004 name_0003 name_0002 name_0001 name_0006 name_0007 6 7
P 7 7 6 5 4 3 2 1 name_0007 name_0006 name_0005 name_0004 name_0003 name_0002 name_0001 name_0007 name_0008 7 8
P 8 8 7 6 5 4 3 2 1 name_0008 name_0007 name_0006 name_0005 name_0004 name_0003 name_0002 name_0001 name_0008 name_0009 8 9
P 9 9 8 7 6 5 4 3 2 name_0009 name_0008 name_0007 name_0006 name_0005 name_0004 name_0003 name_0002 name_0009 name_0010 9 10
P 10 10 9 8 7 6 5 4 3 name_0010 name_0009 name_0008 name_0007 name_0006 name_0005 name_0004 name_0003 name_0010 name_0011 10 11
P 11 11 10 9 8 7 6 5 4 name_0011 name_0010 name_0009 name_0008 name_0007 name_0006 name_0005 name_0004 name_0011 name_0012 11 12
P 12 12 11 10 9 8 7 6 5 name_0012 name_0011 name_0010 name_0009 name_0008 name_0007 name_0006 name_0005 name_0012 name_0013 12 13
P 13 13 12 11 10 9 8 7 6 name_0013 name_0012 name_0011 name_0010 name_0009 name_0008 name_0007 name_0006 name_0013 name_0014 13 14
P 14 14 13 12 11 10 9 8 7 name_0014 name_0013 name_0012 name_0011 name_0010 name_0009 name_0008 name_0007 name_0014 name_0015 14 15
P 15 15 14 13 12 11 10 9 8 name_0015 name_0014 name_0013 name_0012 name_0011 name_0010 name_0009 name_0008 name_0015 name_0016 15 16
P 16 16 15 14 13 12 11 10 9 name_0016 name_0015 name_0014 name_0013 name_0012 name_0011 name_0010 name_0009 name_0016 name_0017 16 17
P 17 17 16 15 14 13 12 11 10 name_0017 name_0016 name_0015 name_0014 name_0013 name_0012 name_0011 name_0010 name_0017 name_0018 17 18
P 18 18 17 16 15 14 13 12 11 name_0018 name_0017 name_0016 name_0015 name_0014 name_0013 name_0012 name_0011 name_0018 name_0019 18 19
P 19 19 18 17 16 15 14 13 12 name_0019 name_0018 name_0017 name_0016 name_0015 name_0014 name_0013 name_0012 name_0019 name_0020 19 20
P 20 20 19 18 17 16 15 14 13 name_0020 name_0019 name_0018 name_0017 name_0016 name_0015 name_0014 name_0013 name_0020 name_0021 20 21
P 21 21 20 19 18 17 16 15 14 name_0021 name_0020 name_0019 name_0018 name_0017 name_0016 name_0015 name_0014 name_0021 name_0022 21 22
P 22 22 21 20 19 18 17 16 15 name_0022 name_0021 name_0020 name_0019 name_0018 name_0017 name_0016 name_0015 name_0022 name_0023 22 23
P 23 23 22 21 20 19 18 17 16 name_0023 name_0022 name_0021 name_0020 name_0019 name_0018 name_0017 name_0016 name_0023 name_0024 23 24
P 24 24 23 22 21 20 19 18 17 name_0024 name_0023 name_0022 name_0021 name_0020 name_0019 name_0018 name_0017 name_0024 name_0025 24 25
P 25 25 24 23 22 21 20 19 18 name_0025 name_0024 name_0023 name_0022 name_0021 name_0020 name_0019 name_0018 name_0025 name_0026 25 26
P 26 26 25 24 23 22 21 20 19 name_0026 name_0025 name_0024 name_0023 name_0022 name_0021 name_0020 name_0019 name_0026 name_0027 26 27
P 27 27 26 25 24 23 22 21 20 name_0027 name_0026 name_0025 name_0024 name_0023 name_0022 name_0021 name_0020 name_0027 name_0028 27 28
P 28 28 27 26 25 24 23 22 21 name_0028 name_0027 name_0026 name_0025 name_0024 name_0023 name_0022 name_0021 name_0028 name_0029 28 29
P 29 29 28 27 26 25 24 23 22 name_0029 name_0028 name_0027 name_0026 name_0025 name_0024 name_0023 name_0022 name_0029 name_0030 29 30
P 30 30 29 28 27 26 25 24 23 name_0030 name_0029 name_0028 name_0027 name_0026 name_0025 name_0024 name_0023 name_0030 name_0031 30 31
P 31 31 30 29 28 27 26 25 24 name_0031 name_0030 name_0029 name_0028 name_0027 name_0026 name_0025 name_0024 name_0031 name_0032 31 32
P 32 32 31 30 29 28 27 26 25 name_0032 name_0031 name_0030 name_0029 name_0028 name_0027 name_0026 name_0025 name_0032 name_0033 32 33
P 33 33 32 31 30 29 28 27 26 name_0033 name_0032 name_0031 name_0030 name_0029 name_0028 name_0027 name_0026 name_0033 name_0034 33 34
P 34 34 33 32 31 30 29 28 27 name_0034 name_0033 name_0032 name_0031 name_0030 name_0029 name_0028 name_0027 name_0034 name_0035 34 35
P 35 35 34 33 32 31 30 29 28 name_0035 name_0034 name_0033 name_0032 name_0031 name_0030 name_0029 name_0028 name_0035 name_0036 35 36
P 36 36 35 34 33 32 31 30 29 name_0036 name_0035 name_0034 name_0033 name_0032 name_0031 name_0030 name_0029 name_0036 name_0037 36 37
P 37 37 36 35 34 33 32 31 30 name_0037 name_0036 name_0035 name_0034 name_0033 name_0032 name_0031 name_0030 name_0037 name_0038 37 38
P 38 38 37 36 35 34 33 32 31 name_0038 name_0037 name_0036 name_0035 name_0034 name_0033 name_0032 name_0031 name_0038 name_0039 38 39
P 39 39 38 37 36 35 34 33 32 name_0039 name_0038 name_0037 name_0036 name_0035 name_0034 name_0033 name_0032 name_0039 name_0040 39 40
P 40 40 39 38 37 36 35 34 33 name_0040 name_0039 name_0038 name_0037 name_0036 name_0035 name_0034 name_0033 name_0040 name_0041 40 41
P 41 41 40 39 38 37 36 35 34 name_0041 name_0040 name_0039 name_0038 name_0037 name_0036 name_0035 name_0034 name_0041 name_0042 41 42
P 42 42 41 40 39 38 37 36 35 name_0042 name_0041 name_0040 name_0039 name_0038 name_0037 name_0036 name_0035 name_0042 name_0043 42 43
P 43 43 42 41 40 39 38 37 36 name_0043 name_0042 name_0041 name_0040 name_0039 name_0038 name_0037 name_0036 name_0043 name_0044 43 44
P 44 44 43 42 41 40 39 38 37 name_0044 name_0043 name_0042 name_0041 name_0040 name_0039 name_0038 name_0037 name_0044 name_0045 44 45
P 45 45 44 43 42 41 40 39 38 name_0045 name_0044 name_0043 name_0042 name_0041 name_0040 name_0039 name_0038 name_0045 name_0046 45 46
P 46 46 45 44 43 42 41 40 39 name_0046 name_0045 name_0044 name_0043 name_0042 name_0041 name_0040 name_0039 name_0046 name_0047 46 47
P 47 47 46 45 44 43 42 41 40 name_0047 name_0046 name_0045 name_0044 name_0043 name_0042 name_0041 name_0040 name_0047 name_0048 47 48
P 48 48 47 46 45 44 43 42 41 name_0048 name_0047 name_0046 name_0045 name_0044 name_0043 name_0042 name_0041 name_0048 name_0049 48 49
P 49 49 48 47 46 45 44 43 42 name_0049 name_0048 name_0047 name_0046 name_0045 name_0044 name_0043 name_0042 name_0049 name_0050 49 50
P 50 50 49 48 47 46 45 44 43 name_0050 name_0049 name_0048 name_0047 name_0046 name_0045 name_0044 name_0043 name_0050 name_0051 50 51
P 51 51 50 49 48 47 46 45 44 name_0051 name_0050 name_0049 name_0048 name_0047 name_0046 name_0045 name_0044 name_0051 name_0052 51 52
P 52 52 51 50 49 48 47 46 45 name_0052 name_0051 name_0050 name_0049 name_0048 name_0047 name_0046 name_0045 name_0052 name_0053 52 53
P 53 53 52 51 50 49 48 47 46 name_0053 name_0052 name_0051 name_0050 name_0049 name_0048 name_0047 name_0046 name_0053 name_0054 53 54
P 54 54 53 52 51 50 49 48 47 name_0054 name_0053 name_0052 name_0051 name_0050 name_0049 name_0048 name_0047 name_0054 name_0055 54 55
P 55 55 54 53 52 51 50 49 48 name_0055 name_0054 name_0053 name_0052 name_0051 name_0050 name_0049 name_0048 name_0055 name_0056 55 56
P 56 56 55 54 53 52 51 50 49 name_0056 name_0055 name_0054 name_0053 name_0052 name_0051 name_0050 name_0049 name_0056 name_0057 56 57
P 57 57 56 55 54 53 52 51 50 name_0057 name_0056 name_0055 name_0054 name_0053 name_0052 name_0051 name_0050 name_0057 name_0058 57 58
P 58 58 57 56 55 54 53 52 51 name_0058 name_0057 name_0056 name_0055 name_0054 name_0053 name_0052 name_0051 name_0058 name_0059 58 59
P 59 59 58 57 56 55 54 53 52 name_0059 name_0058 name_0057 name_0056 name_0055 name_0054 name_0053 name_0052 name_0059 name_0060 59 60
P 60 60 59 58 57 56 55 54 53 name_0060 name_0059 name_0058 name_0057 name_0056 name_0055 name_0054 name_0053 name_0060 name_0061 60 61
P 61 61 60 59 58 57 56 55 54 name_0061 name_0060 name_0059 name_0058 name_0057 name_0056 name_0055 name_0054 name_0061 name_0062 61 62
P 62 62 61 60 59 58 57 56 55 name_0062 name_0061 name_0060 name_0059 name_0058 name_0057 name_0056 name_0055 name_0062 name_0063 62 63
P 63 63 62 61 60 59 58 57 56 name_0063 name_0062 name_0061 name_0060 name_0059 name_0058 name_0057 name_0056 name_0063 name_0064 63 64
P 64 64 63 62 61 60 59 58 57 name_0064 name_0063 name_0062 name_0061 name_0060 name_0059 name_0058 name_0057 name_0064 name_0065 64 65
P 65 65 64 63 62 61 60 59 58 name_0065 name_0064 name_0063 name_0062 name_0061 name_0060 name_0059 name_0058 name_0065 name_0066 65 66
P 66 66 65 64 63 62 61 60 59 name_0066 name_0065 name_0064 name_0063 name_0062 name_0061 name_0060 name_0059 name_0066 name_0067 66 67
P 67 67 66 65 64 63 62 61 60 name_0067 name_0066 name_0065 name_0064 name_0063 name_0062 name_0061 name_0060 name_0067 name_0068 67 68
P 68 68 67 66 65 64 63 62 61 name_0068 name_0067 name_0066 name_0065 name_0064 name_0063 name_0062 name_0061 name_0068 name_0069 68 69
P 69 69 68 67 66 65 64 63 62 name_0069 name_0068 name_0067 name_0066 name_0065 name_0064 name_0063 name_0062 name_0069 name_0070 69 70
P 70 70 69 68 67 66 65 64 63 name_0070 name_0069 name_0068 name_0067 name_0066 name_0065 name_0064 name_0063 name_0070 name_0071 70 71
P 71 71 70 69 68 67 66 65 64 name_0071 name_0070 name_0069 name_0068 name_0067 name_0066 name_0065 name_0064 name_0071 name_0072 71 72
P 72 72 71 70 69 68 67 66 65 name_0072 name_0071 name_0070 name_0069 name_0068 name_0067 name_0066 name_0065 name_0072 name_0073 72 73
P 73 73 72 71 70 69 68 67 66 name_0073 name_0072 name_0071 name_0070 name_0069 name_0068 name_0067 name_0066 name_0073 name_0074 73 74
P 74 74 73 72 71 70 69 68 67 name_0074 name_0073 name_0072 name_0071 name_0070 name_0069 name_0068 name_0067 name_0074 name_0075 74 75
P 75 75 74 73 72 71 70 69 68 name_0075 name_0074 name_0073 name_0072 name_0071 name_0070 name_0069 name_0068 name_0075 name_0076 75 76
P 76 76 75 74 73 72 71 70 69 name_0076 name_0075 name_0074 name_0073 name_0072 name_0071 name_0070 name_0069 name_0076 name_0077 76 77
P 77 77 76 75 74 73 72 71 70 name_0077 name_0076 name_0075 name_0074 name_0073 name_0072 name_0071 name_0070 name_0077 name_0078 77 78
P 78 78 77 76 75 74 73 72 71 name_0078 name_0077 name_0076 name_0075 name_0074 name_0073 name_0072 name_0071 name_0078 name_0079 78 79
P 79 79 78 77 76 75 74 73 72 name_0079 name_0078 name_0077 name_0076 name_0075 name_0074 name_0073 name_0072 name_0079 name_0080 79 80
P 80 80 79 78 77 76 75 74 73 name_0080 name_0079 name_0078 name_0077 name_0076 name_0075 name_0074 name_0073 name_0080 name_0081 80 81
P 81 81 80 79 78 77 76 75 74 name_0081 name_0080 name_0079 name_0078 name_0077 name_0076 name_0075 name_0074 name_0081 name_0082 81 82
P 82 82 81 80 79 78 77 76 75 name_0082 name_0081 name_0080 name_0079 name_0078 name_0077 name_0076 name_0075 name_0082 name_0083 82 83
P 83 83 82 81 80 79 78 77 76 name_0083 name_0082 name_0081 name_0080 name_0079 name_0078 name_0077 name_0076 name_0083 name_0084 83 84
P 84 84 83 82 81 80 79 78 77 name_0084 name_0083 name_0082 name_0081 name_0080 name_0079 name_0078 name_0077 name_0084 name_0085 84 85
P 85 85 84 83 82 81 80 79 78 name_0085 name_0084 name_0083 name_0082 name_0081 name_0080 name_0079 name_0078 name_0085 name_0086 85 86
P 86 86 85 84 83 82 81 80 79 name_0086 name_0085 name_0084 name_0083 name_0082 name_0081 name_0080 name_0079 name_0086 name_0087 86 87
P 87 87 86 85 84 83 82 81 80 name_0087 name_0086 name_0085 name_0084 name_0083 name_0082 name_0081 name_0080 name_0087 name_0088 87 88
P 88 88 87 86 85 84 83 82 81 name_0088 name_0087 name_0086 name_0085 name_0084 name_0083 name_0082 name_0081 name_0088 name_0089 88 89
P 89 89 88 87 86 85 84 83 82 name_0089 name_0088 name_0087 name_0086 name_0085 name_0084 name_0083 name_0082 name_0089 name_0090 89 90
P 90 90 89 88 87 86 85 84 83 name_0090 name_0089 name_0088 name_0087 name_0086 name_0085 name_0084 name_0083 name_0090 name_0091 90 91
P 91 91 90 89 88 87 86 85 84 name_0091 name_0090 name_0089 name_0088 name_0087 name_0086 name_0085 name_0084 name_0091 name_0092 91 92
P 92 92 91 90 89 88 87 86 85 name_0092 name_0091 name_0090 name_0089 name_0088 name_0087 name_0086 name_0085 name_0092 name_0093 92 93
P 93 93 92 91 90 89 88 87 86 name_0093 name_0092 name_0091 name_0090 name_0089 name_0088 name_0087 name_0086 name_0093 name_0094 93 94
P 94 94 93 92 91 90 89 88 87 name_0094 name_0093 name_0092 name_0091 name_0090 name_0089 name_0088 name_0087 name_0094 name_0095 94 95
P 95 95 94 93 92 91 90 89 88 name_0095 name_0094 name_0093 name_0092 name_0091 name_0090 name_0089 name_0088 name_0095 name_0096 95 96
P 96 96 95 94 93 92 91 90 89 name_0096 name_0095 name_0094 name_0093 name_0092 name_0091 name_0090 name_0089 name_0096 name_0097 96 97
P 97 97 96 95 94 93 92 91 90 name_0097 name_0096 name_0095 name_0094 name_0093 name_0092 name_0091 name_0090 name_0097 name_0098 97 98
P 98 98 97 96 95 94 93 92 91 name_0098 name_0097 name_0096 name_0095 name_0094 name_0093 name_0092 name_0091 name_0098 name_0099 98 99
P 99 99 98 97 96 95 94 93 92 name_0099 name_0098 name_0097 name_0096 name_0095 name_0094 name_0093 name_0092 name_0099 name_0100 99 100
So you can take this example and modify it for your needs: add required number of previous data(9-20), change column names, remove unneeded columns.

Drop multiple columns in python

I have 1000 columns in my dataframe, and I want to drop 3rd, 5th, 7th, 9th....999th columns, but need to keep the 1st column. I am not sure if df.drop function can deal with this issue. any ideas? The following code drops all odd columns include 1st column. but I need to keep the 1st column
col_to_drop = df.columns[np.array([i for i in range(df.shape[1]) if i%2!= 1 and i != 1])]
df.drop(col_to_drop, axis=1)
Try this:
df = pd.DataFrame(np.arange(100).reshape(10,-1), columns=[*'ABCDEFGHIJ'])
df.drop(df.columns[np.r_[2,4,5,6,9]], axis=1)
# or df.drop(df.columns[np.array([2,4,5,6,9])], axis=1)
Output:
A B D H I
0 0 1 3 7 8
1 10 11 13 17 18
2 20 21 23 27 28
3 30 31 33 37 38
4 40 41 43 47 48
5 50 51 53 57 58
6 60 61 63 67 68
7 70 71 73 77 78
8 80 81 83 87 88
9 90 91 93 97 98
Update for 1,000 columns.
Drops every odd colum except 1. -> 3,5,7,9...999
col_to_drop = df.columns[np.array([i for i in range(df.shape[1]) if i%2 != 0 and i != 0])]
df.drop(col_to_drop, axis=1)
I think you can try like below:
>>> df
A B C D E
0 A1 B1 C1 D1 E1
1 A2 B2 C2 D2 E2
2 A3 B3 C3 D3 E3
3 A4 B4 C4 D4 E4
4 A5 B5 C5 D5 E5
Drop unwanted columns:
>>> df.drop(ex.columns[[0, 4, 2]], axis = 1, inplace = True)
Result:
>>> df
B D
0 B1 D1
1 B2 D2
2 B3 D3
3 B4 D4
4 B5 D5
Another way around:
>>> df
A B C D E F G H I J K L M N O P
0 24 27 61 40 12 20 52 95 47 23 92 97 86 73 89 84
1 88 33 39 38 87 7 38 31 77 0 66 41 30 34 80 4
2 8 45 68 99 21 66 6 54 28 48 29 18 12 56 30 3
3 22 79 37 1 32 5 53 13 0 57 54 97 71 86 84 12
4 94 19 8 99 91 91 77 90 56 50 41 4 70 36 56 58
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
95 70 22 73 15 41 50 81 38 31 78 94 72 62 56 78 17
96 13 45 40 83 25 24 73 30 67 85 98 63 32 1 70 97
97 24 58 74 64 0 43 36 27 17 76 51 36 85 39 15 77
98 29 3 73 57 39 70 45 53 83 98 92 51 49 45 85 71
99 74 22 77 87 4 16 69 77 39 88 71 73 39 8 63 34
[100 rows x 16 columns]
As you want to drop all the odd columns hence other way around is to opt all the even column then while using .loc indexer as follows.
>>> df.loc[:, ::2]
A C E G I K M O
0 24 61 12 52 47 92 86 89
1 88 39 87 38 77 66 30 80
2 8 68 21 6 28 29 12 30
3 22 37 32 53 0 54 71 84
4 94 8 91 77 56 41 70 56
.. .. .. .. .. .. .. .. ..
95 70 73 41 81 31 94 62 78
96 13 40 25 73 67 98 32 70
97 24 74 0 36 17 51 85 15
98 29 73 39 45 83 92 49 85
99 74 77 4 69 39 71 39 63
[100 rows x 8 columns]
OR
>>> df[df.columns[::2]]
A C E G I K M O
0 24 61 12 52 47 92 86 89
1 88 39 87 38 77 66 30 80
2 8 68 21 6 28 29 12 30
3 22 37 32 53 0 54 71 84
4 94 8 91 77 56 41 70 56
.. .. .. .. .. .. .. .. ..
95 70 73 41 81 31 94 62 78
96 13 40 25 73 67 98 32 70
97 24 74 0 36 17 51 85 15
98 29 73 39 45 83 92 49 85
99 74 77 4 69 39 71 39 63
[100 rows x 8 columns]
A little change from scott Boston's answer works perfectly for this case
col_to_drop = df.columns[np.array([i for i in range(df.shape[1]) if i%2 != 1 and i != 0])]
df.drop(col_to_drop, axis=1)

Sorting a string of numbers into a grid

I'm needing to sort a long list of ID numbers into 'grids' of 8 ID numbers down (8 cells/rows), 6 ID numbers across (or 6 columns long etc), sorted from smallest to largest ID number. When one 'grid' is 'full', the numbers which cannot fit in the first grid should go on to form a second one and so on. The last 4 cells of the last row should be blank. (This is a template for a lab procedure).
ie this is the data I have:
column of ID numbers
and this how I want it to be (but like, 6 of these)
example 'grid'
Here's one method.
Sample data
import pandas as pd
import numpy as np
# Sorted list of string IDs
l = np.arange(0, 631, 1).astype('str')
Code
N = 44
# Ensure we can reshape last group
data = np.concatenate((l, np.repeat('', N-len(l)%N)))
# Split array, make a separate `DataFrame` for each grid.
data = [
pd.DataFrame(np.concatenate((x, np.repeat('', 4))).reshape(8,6))
for x in np.array_split(data, np.arange(N, len(l), N))
]
df = pd.concat(data, ignore_index=True) # If want a single df in the end
Output df:
0 1 2 3 4 5
0 0 1 2 3 4 5
1 6 7 8 9 10 11
2 12 13 14 15 16 17
3 18 19 20 21 22 23
4 24 25 26 27 28 29
5 30 31 32 33 34 35
6 36 37 38 39 40 41
7 42 43
8 44 45 46 47 48 49
9 50 51 52 53 54 55
10 56 57 58 59 60 61
11 62 63 64 65 66 67
12 68 69 70 71 72 73
13 74 75 76 77 78 79
14 80 81 82 83 84 85
15 86 87
16 88 89 90 91 92 93
...
110 608 609 610 611 612 613
111 614 615
112 616 617 618 619 620 621
113 622 623 624 625 626 627
114 628 629 630
115
116
117
118
119
func = lambda lst,n: np.pad(lst, (0,n*(1+len(lst)//n) - len(lst)), 'constant')
rows, cols = 8, 6
arr = np.arange(1, 283, 1) ##np.array(df.A)
new_df = pd.DataFrame(func(arr, rows*cols).reshape(-1,cols))
new_df
0 1 2 3 4 5
0 1 2 3 4 5 6
1 7 8 9 10 11 12
2 13 14 15 16 17 18
3 19 20 21 22 23 24
4 25 26 27 28 29 30
5 31 32 33 34 35 36
6 37 38 39 40 41 42
7 43 44 45 46 47 48
8 49 50 51 52 53 54
9 55 56 57 58 59 60
10 61 62 63 64 65 66
11 67 68 69 70 71 72
12 73 74 75 76 77 78
13 79 80 81 82 83 84
14 85 86 87 88 89 90
15 91 92 93 94 95 96
16 97 98 99 100 101 102
17 103 104 105 106 107 108
18 109 110 111 112 113 114
19 115 116 117 118 119 120
20 121 122 123 124 125 126
21 127 128 129 130 131 132
22 133 134 135 136 137 138
23 139 140 141 142 143 144
24 145 146 147 148 149 150
25 151 152 153 154 155 156
26 157 158 159 160 161 162
27 163 164 165 166 167 168
28 169 170 171 172 173 174
29 175 176 177 178 179 180
30 181 182 183 184 185 186
31 187 188 189 190 191 192
32 193 194 195 196 197 198
33 199 200 201 202 203 204
34 205 206 207 208 209 210
35 211 212 213 214 215 216
36 217 218 219 220 221 222
37 223 224 225 226 227 228
38 229 230 231 232 233 234
39 235 236 237 238 239 240
40 241 242 243 244 245 246
41 247 248 249 250 251 252
42 253 254 255 256 257 258
43 259 260 261 262 263 264
44 265 266 267 268 269 270
45 271 272 273 274 275 276
46 277 278 279 280 281 282
47 0 0 0 0 0 0
I think it's better to save this dataframe into an excel worksheet and then remove the last padded zeros manually. Hope this helped

SQL SERVER order by random in series

I want to display records in random order, the simplest answer is order by NEWID(), that's fine.
I have EmployeesID from 1 to 100. The output needs to be displayed in random series, i.e.
30.31.32.33...40..20.21.22..30..80.81.82..90.1.2.3..10..and so on..
If there are 100 records, it should be divided into 10 parts and every part displays randomly.
CODE I tried is:
select empId,empName from employees
where empID between 1 and 100
order by NEWID()
INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
OUTPUT
50 51 52 53 54 55 56 57 58 59 60 90 91 92 93 94 95 96 97 98 99 100 30 31 32 33 34 35 36 37 38 39 40 10 11 12 13 14 15 16 17 18 19 20 20 21 22 23 24 25 26 27 28 29 30 70 71 72 73 74 75 76 77 78 79 80 60 61 62 63 64 65 66 67 68 69 70 1 2 3 4 5 6 7 8 9 10 40 41 42 43 44 45 46 47 48 49 50 80 81 82 83 84 85 86 87 88 89 90
I will use NORTHWND.[dbo].[Employees], it got 9 employees.
USE tempdb;
GO
SET NOCOUNT ON;
CREATE TABLE #SAMPLE_DATA
(
empID int,
ni uniqueidentifier
);
INSERT INTO #SAMPLE_DATA(empID, ni )
SELECT [EmployeeID], NEWID()
FROM NORTHWND.[dbo].[Employees]
ORDER BY NEWID()
DECLARE #Values VARCHAR(1000)
SELECT #Values = COALESCE(#Values + ' ', '') + CAST(empID AS varchar(50))
FROM #SAMPLE_DATA
ORDER BY ni
SELECT #Values;
DROP TABLE #SAMPLE_DATA;
Output:
2 6 4 1 3 9 7 8 5
Next run:
8 5 3 1 6 4 7 9 2
etc