Home > Net >  How to create a new Dataframe based on another Dataframe
How to create a new Dataframe based on another Dataframe

Time:02-01

Below is a dataframes for example :

Employee= pd.DataFrame(
    {"EmployeeKey": ["1", "2", "3"], 
     "End_Contract": [None, "2022-02-02", None],
     "Contract": ["35", "35", "35"]}
)

I use a calendar like the following :

Calendar = pd.DataFrame({'Year':[2022,2022,2022], 
                         'Month ':[1,2,3], 
                         'Working day':[21,20,21]})

I try to fill a new dataframe from the last two dataframes.

the final result I'm trying to find

| EmployeeKey | EndContract | Contract| Year   | Month    |  Working day     | 
| --------    | ----------- |-------  |------- | -------  |  --------------    
| 1           | None        |    35   | 2022   |  1       |      21          |
| 1           | None        |    35   | 2022   |  2       |      20          |
| 1           | None        |    35   | 2022   |  3       |      21          |
| 2           | 2022-02-02  |    35   | 2022   |  1       |      21          |
| 2           | 2022-02-02  |    35   | 2022   |  2       |      20          |
| 2           | 2022-02-02  |    35   | 2022   |  3       |      21          |
| 3           | None        |    35   | 2022   |  1       |      21          |
| 3           | None        |    35   | 2022   |  2       |      20          |
| 3           | None        |    35   | 2022   |  3       |      21          |

I can't find a way to get this result !

CodePudding user response:

I don't know your splicing rules, but I recommend you take a look at "pd.merge" enter image description here

  •  Tags:  
  • Related