简体   繁体   中英

Python - Building a summary data frame from a dataframe

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.

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 |
+----+----------------------+------------------+--------------------+----------------+-----------------+----------------+---------------+------------+-------------+------------+-------------+

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM