task_boiler
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
task_boiler [2022/03/18 22:59] – [Task] raju | task_boiler [2024/01/23 22:55] (current) – raju | ||
---|---|---|---|
Line 4: | Line 4: | ||
Start the first heading with " | Start the first heading with " | ||
- | ===== convert a dictionary of dataframes to a big dataframe ===== | ||
- | ==== Task ==== | ||
- | Given a dictionary strings to dataframes, create an expanded dataframe by putting the keys into their own column. For example, given | ||
- | < | ||
- | {' | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | ' | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | We want | ||
- | < | ||
- | ticker | ||
- | 0 | ||
- | 1 | ||
- | 2 | ||
- | 3 | ||
- | 4 | ||
- | 5 | ||
- | 6 | ||
- | 7 | ||
- | </ | ||
- | |||
- | ==== Solution ==== | ||
- | < | ||
- | pd.concat(dict_df, | ||
- | </ | ||
- | Using the example above | ||
- | < | ||
- | $ ipython | ||
- | Python 3.6.12 |Anaconda, Inc.| (default, Sep 9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)] | ||
- | Type ' | ||
- | IPython 7.16.1 -- An enhanced Interactive Python. Type '?' | ||
- | |||
- | In [1]: | ||
- | import pandas as pd | ||
- | costco_earnings = pd.DataFrame({ | ||
- | ' | ||
- | ' | ||
- | }) | ||
- | costco_earnings | ||
- | Out[1]: | ||
- | | ||
- | 0 202102 | ||
- | 1 202105 | ||
- | 2 202108 | ||
- | 3 202111 | ||
- | |||
- | In [2]: | ||
- | copart_earnings = pd.DataFrame({ | ||
- | ' | ||
- | ' | ||
- | }) | ||
- | copart_earnings | ||
- | Out[2]: | ||
- | | ||
- | 0 202104 | ||
- | 1 202107 | ||
- | 2 202110 | ||
- | 3 202201 | ||
- | |||
- | In [3]: | ||
- | dict_df = {' | ||
- | dict_df | ||
- | Out[3]: | ||
- | {' | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | ' | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | |||
- | In [4]: | ||
- | expanded_df = pd.concat(dict_df, | ||
- | expanded_df | ||
- | Out[4]: | ||
- | ticker | ||
- | 0 | ||
- | 1 | ||
- | 2 | ||
- | 3 | ||
- | 4 | ||
- | 5 | ||
- | 6 | ||
- | 7 | ||
- | </ | ||
- | To see how it works | ||
- | < | ||
- | In [5]: | ||
- | pd.concat(dict_df, | ||
- | Out[5]: | ||
- | fiscal_quarter_end | ||
- | COST 0 202102 | ||
- | | ||
- | | ||
- | | ||
- | CPRT 0 202104 | ||
- | | ||
- | | ||
- | | ||
- | |||
- | In [6]: | ||
- | pd.concat(dict_df, | ||
- | Out[6]: | ||
- | level_0 | ||
- | 0 COST 0 202102 | ||
- | 1 COST 1 202105 | ||
- | 2 COST 2 202108 | ||
- | 3 COST 3 202111 | ||
- | 4 CPRT 0 202104 | ||
- | 5 CPRT 1 202107 | ||
- | 6 CPRT 2 202110 | ||
- | 7 CPRT 3 202201 | ||
- | |||
- | In [7]: | ||
- | pd.concat(dict_df, | ||
- | Out[7]: | ||
- | level_0 | ||
- | 0 COST 202102 | ||
- | 1 COST 202105 | ||
- | 2 COST 202108 | ||
- | 3 COST 202111 | ||
- | 4 CPRT 202104 | ||
- | 5 CPRT 202107 | ||
- | 6 CPRT 202110 | ||
- | 7 CPRT 202201 | ||
- | |||
- | In [8]: | ||
- | pd.concat(dict_df, | ||
- | Out[8]: | ||
- | ticker | ||
- | 0 | ||
- | 1 | ||
- | 2 | ||
- | 3 | ||
- | 4 | ||
- | 5 | ||
- | 6 | ||
- | 7 | ||
- | </ |
task_boiler.1647644383.txt.gz · Last modified: 2022/03/18 22:59 by raju