1

I have a dataframe like this,

DataFrame_A

Employee ID   A_ Status  C_Code  TestCol   Result_A  Result_B
20000         Yes        USA      asdasdq  True      False
20001         No         BRA      asdasdw  True      True
200002                   USA      asdasda  True      True 
200003        asda       MEX      asdasar  False     False

In this dataframe, Result_A and Result_B are Boolean columns.

I want to build a summary dataframe through a function, so that I can re-use.

I need the following columns in my dataframe and the output for Result_A looks as below and the Result_B another Boolean column will be the next row of the summary dataframe.

 Name of the Column     No. of Records     No. of Employees    True_Records    False_Records     A_Status_Yes  A_Status_No     Mex_True      Mex_False      USA_True     USA_False
         Result_A              4               4                    3                     1                1            1               0            1              2              2  

Also to note, Employee ID may sometimes be EMPLOYEE ID or Employee_ID or EMPLOYEE_ID or EMPL_ID. So, a list need to be inside python and only one of them will be present inside the function

In real time, I have 25 data frames, therefore looking for a function that I can reuse and append.

Kindly help me with this.

1
  • Not sure what you need ... Commented Apr 19, 2019 at 1:29

1 Answer 1

2

I think I got what you want:

1- Re-create your df:

df = pd.DataFrame({"Employee ID": [20000, 20001, 200002, 200003],
                  "A_ Status": ["Yes", "No", np.nan, "asda"],
                  "C_Code": ["USA", "BRA", "USA", "MEX"],
                  "TestCol": ["asdasdq", "asdasdw", "asdasda", "asdasar"],
                  "Result_A": [True, True, True, False],
                  "Result_B": [False, True, True, False]}, 
                  columns=["Employee ID", "A_ Status", "C_Code", "TestCol", "Result_A", "Result_B"])

2- Create second dataframe df2:

df2 = pd.DataFrame(columns=["Name of the Column","No. of Records","No. of Employees","True_Records","False_Records","A_Status_Yes","A_Status_No","Mex_True","Mex_False","USA_True","USA_False"])

3- Compute results:

for column in df.columns[4:]: # For each columns of name pattern `Result_xx`
    print(column)
    a = [column,
        len(df["Employee ID"]), # Not sure about this one
        len(df["Employee ID"]),
        len(df[df[column] == True]),
        len(df[df[column] == False]),
        len(df[df["A_ Status"] == "Yes"]),
        len(df[df["A_ Status"] == "No"]),
        len(df[(df["C_Code"] == "MEX") & (df[column] == True)]),
        len(df[(df["C_Code"] == "MEX") & (df[column] == False)]),
        len(df[(df["C_Code"] == "USA") & (df[column] == True)]),
        len(df[(df["C_Code"] == "USA") & (df[column] == False)])
       ] # Create line as list

    df2.loc[len(df2), :] = a # Append line

4- Results:

+----+----------------------+------------------+--------------------+----------------+-----------------+----------------+---------------+------------+-------------+------------+-------------+
|    | Name of the Column   |   No. of Records |   No. of Employees |   True_Records |   False_Records |   A_Status_Yes |   A_Status_No |   Mex_True |   Mex_False |   USA_True |   USA_False |
|----+----------------------+------------------+--------------------+----------------+-----------------+----------------+---------------+------------+-------------+------------+-------------|
|  0 | Result_A             |                4 |                  4 |              3 |               1 |              1 |             1 |          0 |           1 |          2 |           0 |
|  1 | Result_B             |                4 |                  4 |              2 |               2 |              1 |             1 |          0 |           1 |          1 |           1 |
+----+----------------------+------------------+--------------------+----------------+-----------------+----------------+---------------+------------+-------------+------------+-------------+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.