User Tools

Site Tools


task_boiler

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
task_boiler [2022/03/18 23:29] – [convert a dictionary of dataframes to a big dataframe] rajutask_boiler [2024/01/22 22:59] – [Issue] raju
Line 4: Line 4:
 Start the first heading with "=====" Start the first heading with "====="
  
-===== Convert a dictionary of dataframes to a big dataframe ===== +===== blocked by barrons ===== 
-==== Task ==== +==== Issue ==== 
-Given a dictionary strings to dataframes, create an expanded dataframe by putting the keys into their own columnFor example, given+barrons.com is giving the following error.
 <code> <code>
-{'COST':    fiscal_quarter_end  reported_date +You have been blocked.
-              202102       20210304 +
-              202105       20210527 +
-              202108       20210923 +
-              202111       20211209, +
- 'CPRT':    fiscal_quarter_end  reported_date +
-              202104       20210519 +
-              202107       20210908 +
-              202110       20211117 +
-              202201       20220216} +
-</code> +
-We want +
-<code> +
-  ticker  fiscal_quarter_end  reported_date +
-0   COST              202102       20210304 +
-1   COST              202105       20210527 +
-2   COST              202108       20210923 +
-3   COST              202111       20211209 +
-4   CPRT              202104       20210519 +
-5   CPRT              202107       20210908 +
-6   CPRT              202110       20211117 +
-7   CPRT              202201       20220216 +
-</code>+
  
-==== Solution ==== +Why? 
-<code> +Something about the behaviour of the browser has caught our 
-def dict_df_to_df(dict_df, key_col_name): +attention.
-    df = ( +
-        pd.concat(dict_df, axis=0)\ +
-        .reset_index()\ +
-        .drop('level_1', axis=1)\ +
-        .rename({'level_0':key_col_name}, axis=1) +
-    ) +
-    return df +
-</code> +
-Using the example above +
-<code> +
-$ ipython+
  
-In [1]+There are various possible explanations for this
-import pandas as pd +* You are browsing and clicking at a speed much faster than 
-costco_earnings = pd.DataFrame({ +  expected of a human being
-  'fiscal_quarter_end': [202102, 202105, 202108, 202111], +* Something is preventing JavaScript from working on your 
-  'reported_date': [20210304, 20210527, 20210923, 20211209] +  computer. 
-}) +* There is a robot on the same network (IP WW.XX.YY.ZZas you.
-costco_earnings +
-Out[1]: +
-   fiscal_quarter_end  reported_date +
-0              202102       20210304 +
-1              202105       20210527 +
-2              202108       20210923 +
-3              202111       20211209+
  
-In [2]: +Having problems accessing the site? Submit feedback
-copart_earnings = pd.DataFrame({ +IDa-b-c-d-e
-  'fiscal_quarter_end'[202104, 202107, 202110, 202201], +
-  'reported_date': [20210519, 20210908, 20211117, 20220216] +
-}) +
-copart_earnings +
-Out[2]: +
-   fiscal_quarter_end  reported_date +
-0              202104       20210519 +
-1              202107       20210908 +
-2              202110       20211117 +
-3              202201       20220216 +
- +
-In [3]: +
-dict_df = {'COST': costco_earnings, 'CPRT': copart_earnings} +
-dict_df +
-Out[3]: +
-{'COST':    fiscal_quarter_end  reported_date +
-              202102       20210304 +
-              202105       20210527 +
-              202108       20210923 +
-              202111       20211209, +
- 'CPRT':    fiscal_quarter_end  reported_date +
-              202104       20210519 +
-              202107       20210908 +
-              202110       20211117 +
-              202201       20220216}+
 </code> </code>
  
-<code> +==== Solution ==== 
-def dict_df_to_df(dict_df, key_col_name): +Click on the extensions button in the top right 
-    df = ( +-> Bypass Paywalls Clean -> settings button -> Options 
-        pd.concat(dict_df, axis=0)\ +-> scroll down to "BPC settings" section 
-        .reset_index()\ +-> check "Barron's - no Googlebot (http error 500)" option 
-        .drop('level_1', axis=1)\ +-Save
-        .rename({'level_0':key_col_name}, axis=1+
-    ) +
-    return df +
-</code>+
  
-<code> +==== System Information ==== 
-In [5]: +  * bypass paywalls clean extension version: 3.5.2.1 (latest asof 2024-01-22
-expanded_df = dict_df_to_df(dict_df, 'ticker'+  * google chrome120.0.6099.200 
-expanded_df +  * OS: Windows 10 Enterprise
-Out[5]+
-  ticker  fiscal_quarter_end  reported_date +
-0   COST              202102       20210304 +
-1   COST              202105       20210527 +
-2   COST              202108       20210923 +
-3   COST              202111       20211209 +
-4   CPRT              202104       20210519 +
-5   CPRT              202107       20210908 +
-6   CPRT              202110       20211117 +
-7   CPRT              202201       20220216 +
-</code>+
  
-To see how it works 
-<code> 
-In [6]: 
-pd.concat(dict_df, axis=0) 
-Out[6]: 
-        fiscal_quarter_end  reported_date 
-COST 0              202102       20210304 
-                  202105       20210527 
-                  202108       20210923 
-                  202111       20211209 
-CPRT 0              202104       20210519 
-                  202107       20210908 
-                  202110       20211117 
-                  202201       20220216 
- 
-In [7]: 
-pd.concat(dict_df, axis=0)\ 
-.reset_index() 
-Out[7]: 
-  level_0  level_1  fiscal_quarter_end  reported_date 
-0    COST        0              202102       20210304 
-1    COST        1              202105       20210527 
-2    COST        2              202108       20210923 
-3    COST        3              202111       20211209 
-4    CPRT        0              202104       20210519 
-5    CPRT        1              202107       20210908 
-6    CPRT        2              202110       20211117 
-7    CPRT        3              202201       20220216 
- 
-In [8]: 
-pd.concat(dict_df, axis=0)\ 
-.reset_index()\ 
-.drop('level_1', axis=1) 
-Out[8]: 
-  level_0  fiscal_quarter_end  reported_date 
-0    COST              202102       20210304 
-1    COST              202105       20210527 
-2    COST              202108       20210923 
-3    COST              202111       20211209 
-4    CPRT              202104       20210519 
-5    CPRT              202107       20210908 
-6    CPRT              202110       20211117 
-7    CPRT              202201       20220216 
- 
-In [9]: 
-pd.concat(dict_df, axis=0)\ 
-.reset_index()\ 
-.drop('level_1', axis=1)\ 
-.rename({'level_0':'ticker'}, axis=1) 
-Out[9]: 
-  ticker  fiscal_quarter_end  reported_date 
-0   COST              202102       20210304 
-1   COST              202105       20210527 
-2   COST              202108       20210923 
-3   COST              202111       20211209 
-4   CPRT              202104       20210519 
-5   CPRT              202107       20210908 
-6   CPRT              202110       20211117 
-7   CPRT              202201       20220216 
-</code> 
task_boiler.txt · Last modified: 2024/01/23 22:55 by raju