I made a bunch of dataframes that are organized like this (lets call this df):
2014-12-31 2013-12-31 2012-12-31 2011-12-31
After Tax ROE 32 11 318 114
Cash Ratio 91 126 41 159
Current Ratio 152 188 97 195
Gross Margin 28 23 7 30
Operating Margin 6 3 95 123
Pre-Tax Margin 9 4 96 124
Pre-Tax ROE 31 11 318 113
Profit Margin 9 4 96 125
Quick Ratio 107 137 48 169
I wrote a script to scrape the NASDAQ site and make a bunch of these all for different stocks. I want to be able to compare these ratios for a year, for different stocks in this format:
2014
AAPL GOOG TSLA
ratio int int int
ratio int int int
ratio int int int
ratio int int int
I know I can just reference the columns like this df[[0]]
to get the column out for 2014 for that particular dataframe.
But I want to index based on the year so it always works no matter how the columns are oriented. I made the column headings for df datetime objects specifically for that purpose. How do I do that?
You can have the columns as a DateTimeIndex which has more flexible selection.
For example:
import pandas as pd
import numpy as np
df = pd.DataFrame(
np.arange(10).reshape((5, 2)),
columns=pd.DatetimeIndex(['2014-04-14', '2015-05-15']))
print(df['2014'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.