Using Complete Function #121
Answered
by
pwwang
kellygfcengineering
asked this question in
Q&A
-
Hello! I have been trying all day to fill a data frame based on combinations of 3 variables. This is how I previously did this in R: tidyr::complete(Date, BOM, Work_Area, fill=list(Quantity=0)) Which works swimmingly. How would I go about this using the complete function in python using datar's complete function? |
Beta Was this translation helpful? Give feedback.
Answered by
pwwang
May 16, 2022
Replies: 2 comments
-
Is this what you wanted: >>> from datar.all import c, f, tibble, complete
>>>
>>> df = tibble(
... Date=c("2020-01-01", "2020-01-02", "2020-01-03"),
... BOM=c(1, 2, 3),
... Work_Area=c("New York", "Arizona", "California"),
... Quality=c(4, 5, 6),
... )
>>>
>>> complete(df, f.Date, f.BOM, f.Work_Area, fill={"Quality": 0})
Date BOM Work_Area Quality
<object> <int64> <object> <float64>
0 2020-01-01 1 Arizona 0.0
1 2020-01-01 1 California 0.0
2 2020-01-01 1 New York 4.0
3 2020-01-01 2 Arizona 0.0
4 2020-01-01 2 California 0.0
5 2020-01-01 2 New York 0.0
6 2020-01-01 3 Arizona 0.0
7 2020-01-01 3 California 0.0
8 2020-01-01 3 New York 0.0
9 2020-01-02 1 Arizona 0.0
10 2020-01-02 1 California 0.0
11 2020-01-02 1 New York 0.0
12 2020-01-02 2 Arizona 5.0
13 2020-01-02 2 California 0.0
14 2020-01-02 2 New York 0.0
15 2020-01-02 3 Arizona 0.0
16 2020-01-02 3 California 0.0
17 2020-01-02 3 New York 0.0
18 2020-01-03 1 Arizona 0.0
19 2020-01-03 1 California 0.0
20 2020-01-03 1 New York 0.0
21 2020-01-03 2 Arizona 0.0
22 2020-01-03 2 California 0.0
23 2020-01-03 2 New York 0.0
24 2020-01-03 3 Arizona 0.0
25 2020-01-03 3 California 6.0
26 2020-01-03 3 New York 0.0 With R: r$> library(dplyr)
r$> library(tidyr)
r$> df = tibble(
Date=c("2020-01-01", "2020-01-02", "2020-01-03"),
BOM=c(1, 2, 3),
Work_Area=c("New York", "Arizona", "California"),
Quality=c(4, 5, 6),
)
r$> out = complete(df, Date, BOM, Work_Area, fill=list(Quality=0))
r$> print(out, n=27)
# A tibble: 27 × 4
Date BOM Work_Area Quality
<chr> <dbl> <chr> <dbl>
1 2020-01-01 1 Arizona 0
2 2020-01-01 1 California 0
3 2020-01-01 1 New York 4
4 2020-01-01 2 Arizona 0
5 2020-01-01 2 California 0
6 2020-01-01 2 New York 0
7 2020-01-01 3 Arizona 0
8 2020-01-01 3 California 0
9 2020-01-01 3 New York 0
10 2020-01-02 1 Arizona 0
11 2020-01-02 1 California 0
12 2020-01-02 1 New York 0
13 2020-01-02 2 Arizona 5
14 2020-01-02 2 California 0
15 2020-01-02 2 New York 0
16 2020-01-02 3 Arizona 0
17 2020-01-02 3 California 0
18 2020-01-02 3 New York 0
19 2020-01-03 1 Arizona 0
20 2020-01-03 1 California 0
21 2020-01-03 1 New York 0
22 2020-01-03 2 Arizona 0
23 2020-01-03 2 California 0
24 2020-01-03 2 New York 0
25 2020-01-03 3 Arizona 0
26 2020-01-03 3 California 6
27 2020-01-03 3 New York 0 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
kellygfcengineering
-
Ahhh, yes! thank you!! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this what you wanted: