python - Pivoting a Pandas dataframe with a gapless daterange as index -
Question: As a condition with the title, how do I get the desired results given below ? Sample data: Desired result: Try efforts Key: The related link was examined (which is not present in this issue Comes in): Note: < Strong> Updates: Apparently, the client-order pairs are repeating the dates later, unlike Note 2 above, which is messing up my data (one million , To check only 20,000 rows in PFTT) and Uri. Answer to the DSM (and my initial hump) is correct that the solution to the smoke is correct. Update 2: Strong> Pgbacking on the north of DSM but for every customer using gives the following output On a revised table: Good for me. Something like that - whatever you tried, honestly looks - works for me : returns:
Date Client Order 0 1/30/1987 AAA on 111 1 3/28/1987 BBB ON 112 2 3/28/1987 CCC ON 113 3 3/28/1987 AAA ON. 114 4 3/31/1987 DDD ON 115 5 3/31/1987 BBB ON. 116 6 3/31/1987 EEE ON. 117 7 4/1/1987 FFF ON 118 8 4/1/1987 CCC ON 119 9 4/1/1987 AAA On 120 10 4/2/1987 DDD ON 121
AAA BBB CCC DDEEEEFFF 1987-01-30 111 No NN nor NN nor NN 1987 -01-31 na nn na na na na na naa 1987-02-01 na nn na na na na na ... ... 1987-03-28 114 112 113 No Nn na na ... 1987 -03-31 Nain 116 NN 115 117 NN 1987 -04-01 120 NNN 119 NN NAN 118 1987-04-02 NN NA NA 121 NN NAN
# Df_p = df.pivot (index = 'date', column = 'customer', value = 'command') # Around There, but I do not want to repeat every cell again to get the order number. Df_symbol = 'df [' client '] df_symbol_unique = set (df_symbol) index = pd.date_range (df [' date ']. IT [0], df [' date ']. Iat [-1]) df_new = pd.DataFrame (NPNAN, Index = Index, Column = Sorted (List (DF_SImbL_Anique)) # AAA BBB CCC DD EEE FFF # 1987-01-30 NN No NN No NN No EN # 1 87-01-31 NN Na na na na na na # 1987-02-01 na nn na nn na nn nayn # ...
Date <
index .
pivot_table , instead Df [date]] df_p = df.pivot_table (line = "date", cols = "client", values = "Order", agfunk = lane) df_p = df_p.reindex (pd.date_range (df_p.index.min (), df_p.index.max ()))
Customer AAA BBB CCC DD EEE FFF 1987-01-30 1 N N NA NA NA NN NO 1987-01-31 NAN NA NA NA NA NA No 1987-02-01 Naen na nnnnnnnnn 1987-02-02 na nn na nay na na na na 1987-02-03 NN na nay na na na na [5 Punk Tea x 6 column] Client AAA BBB CCC DDD EEE FFF 1987-03-29 No EN NA N NA NA NA N NA NO 1987-03-30 NA N NA NA NA NA NA Nain 1987-03-31 Nain 1 Nain 1 1 Nain 1987-04- 01 2 Na NNN 1 1987-04-02 Nain Nn 1 NN N [5 Rounds x 6 Column] [Finishes in 1.4 S]
& gt; & Gt; & Gt; Df Date Customer Order 0 1/30/1987 AAA ON 111 1 3/28/1987 BBB ON 112 2 3/28/1987 CCC ON 113 3 3/28/1987 AAA On 114 4 3/31/1987 DDD ON 115 5 3/31/1987 BBB ON. 116 6 3/31/1987 EEE ON. 117 7 4/1/1987 FFF ON 118 8 4/1/1987 CCC ON 119 9 4/1/1987 AAA ON. 120 10 4/2/1987 DDD ON 121 & gt; & Gt; & Gt; df ["date"] = pd.to_datetime (df ["date"])> gt; & Gt; & Gt; DF ["Order"] = DF ["Order"]. Str.split (). Str [-1]> gt; & Gt; & Gt; Df_p = df.pivot (index = "date", column = "customer", value = "command") & gt; & Gt; & Gt; Df_p = df_p.reindex (pd.date_range (df_p.index.min (), df_p.index.max ())
& Gt; & gt; & gt; Df_p.head () Client AAA BBB CCC DDD EEE FFF1987-01-30 111 NAN NO NN NAND NAN NO 1987-01-31 NAN NA NA NA NA NA No 1987-02-01 na nn na na na na na na na 1987-02-02 na nn na nn na nn na nn 1987-02-03 na nn na nn na na na & gt; & gt; & gt; Df_p.tail () Client AAA Bbb CCC DDD EEE FFF 1987-03-29 Nien nor NN No NN No NN 1987-03-30 No NN No NN No En NO NN 1987-03-31 Naan 116 Anaan 115,117 Anaan L987-04-0l 120 Anaan 119 Anaan Naan 118 L987-04-02 Anaan No NN 121 Anaan Anaan
Comments
Post a Comment