After Sort in Python how to create new Index field - sorting

After I sort my data, I am trying to create a new index field.
Here is my code:
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
So with my code above the output looks like this:
Col_A Col_B Col_C
2 11 46 15
3 23 87 81
0 38 125 56
1 98 23 150
I need to create a new column called "Col_D" as seen below:
Col_A Col_B Col_C Col_D
2 11 46 15 1
3 23 87 81 2
0 38 125 56 3
1 98 23 150 4
Also, I am going to be using this on many data sets. Is there a way to do this without specifying a range?

Here you go
import pandas as pd
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
mydata_3["Col_D"] = range(len(mydata_3.index))
print(mydata_3)

Related

Get Total count

I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32
Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!

Adding new column and inserting values to an exisitng excelsheet using SSIS/SSRS

I have an existing excel template as like below :
DATE 7/28/2016 7/29/2016 July MTD YTD
Call Activity
IB_Calls_Offered 22 29 52 52 52
IB_Calls_Answered 22 29 52 52 52
Sale 6 3 9 9 9
Everyday when my SP get executed , I need to add a extra column (as Current Date) and fill the corresponding data.
Kindly Suggest me how to do it by using SSIS/SSRS

COUNT returning NULL, should return 0

I have a simple query for the table:
Person id Organization id employee_nam age busines_group_id
123 Zuyo 10 John 30 81
2457 Zuyo 10 Geet 69 81
56 Ghiya 12 paul 20 81
frei 13 81
SELECT
COUNT(DISTINCT ped.person_id)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
SELECT
NVL(COUNT(DISTINCT ped.person_id), 0)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
p_org_id is the parameter which I am passing which can be 10, 12, or 13.
COUNT returns 2 for id 10. 1 for id 12. but is returning NULL for id 13.
I want 0 to be returned in this case.
NVL and CASE are also not working.
try MAX:
nvl(max(count(DISTINCT ped.person_id)),0)

update from temp table to original table

I have two tables one is original table and second one is temp table. Temp table having the correct record. Unique column is cust_id. My table structure is
Customer table
cust_id amount
12 100
13 120
14 130
15 250
20 70
25 110
28 900
temp table
cust_id amount
12 300
13 190
14 110
15 240
20 30
25 210
28 500
I want to update the record from temp table to customer orignal table using customer id.
It can be done using merge statement.
merge into original_table ot
using temp_table tp
on (ot.cust_id = tp.cust_id)
when matched
then update set ot.amount = tp. amount

Hive: Joining tables with different scenarios

I have a question on joining tables in a different scenario. Please find the sample tables below.
Capacity of expected table row 3-5 should be repeated as table 2 does not have those fields.
could anyone please help to get expected table?
Table 1:
No ProjectID Capacity
1 514 4
2 418 10
3 418 30
4 401 40
5 502 41
Table2:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
Expected Table:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
3 418 30 30
4 401 40 40
5 502 41 41
1.Do left outer join
2.For the values not matching take them from table 1 with if condition.
select t1.no,t1.projectid,t1.capacity1,if(t2.capacity2 is null,t1.capacity,t2.capacity)
from table1 t1 left outer join table2 t2 on t1.no=t2.no
I think above query meets your requirement let me know if need any more help.

Resources