import pandas as pd
fund_data = [['2016-04-01', 121.55], ['2016-03-01', 115.76], ['2016-02-01', 110.25], ['2016-01-01', 100]]
fund_data_frame = pd.DataFrame(fund_data, columns=['Date', 'Value'])
fund_data_frame.head()
Date | Value | |
---|---|---|
0 | 2016-04-01 | 121.55 |
1 | 2016-03-01 | 115.76 |
2 | 2016-02-01 | 110.25 |
3 | 2016-01-01 | 100.00 |
returns = pd.Series([0.05] * 4)
returns
0 0.05 1 0.05 2 0.05 3 0.05 dtype: float64
fund_data_frame['Return'] = returns
fund_data_frame.head()
Date | Value | Return | |
---|---|---|---|
0 | 2016-04-01 | 121.55 | 0.05 |
1 | 2016-03-01 | 115.76 | 0.05 |
2 | 2016-02-01 | 110.25 | 0.05 |
3 | 2016-01-01 | 100.00 | 0.05 |
# Note that this is not an in-place change - just a returned copy
fund_data_frame.drop('Value', axis=1)
Date | Return | |
---|---|---|
0 | 2016-04-01 | 0.05 |
1 | 2016-03-01 | 0.05 |
2 | 2016-02-01 | 0.05 |
3 | 2016-01-01 | 0.05 |
fund_data_frame['Return'] = fund_data_frame['Return'] + pd.Series([0.05]*4)
fund_data_frame
Date | Value | Return | |
---|---|---|---|
0 | 2016-04-01 | 121.55 | 0.1 |
1 | 2016-03-01 | 115.76 | 0.1 |
2 | 2016-02-01 | 110.25 | 0.1 |
3 | 2016-01-01 | 100.00 | 0.1 |
fund_data_frame['Return'] = fund_data_frame['Return'] / 2
fund_data_frame
Date | Value | Return | |
---|---|---|---|
0 | 2016-04-01 | 121.55 | 0.05 |
1 | 2016-03-01 | 115.76 | 0.05 |
2 | 2016-02-01 | 110.25 | 0.05 |
3 | 2016-01-01 | 100.00 | 0.05 |
And let's say we want the row indicies to not be reversed.
fund_data_frame = fund_data_frame[::-1]
fund_data_frame.index = range(len(fund_data_frame))
fund_data_frame
Date | Value | Return | |
---|---|---|---|
0 | 2016-01-01 | 100.00 | 0.05 |
1 | 2016-02-01 | 110.25 | 0.05 |
2 | 2016-03-01 | 115.76 | 0.05 |
3 | 2016-04-01 | 121.55 | 0.05 |
fund_data_frame[fund_data_frame.Value > 111]
Date | Value | Return | |
---|---|---|---|
2 | 2016-03-01 | 115.76 | 0.05 |
3 | 2016-04-01 | 121.55 | 0.05 |
fund_data_frame.ix[[0,3]]
Date | Value | Return | |
---|---|---|---|
0 | 2016-01-01 | 100.00 | 0.05 |
3 | 2016-04-01 | 121.55 | 0.05 |
fund_data_frame = fund_data_frame.append({'Date': '2016-05-01', 'Value': 130, 'Return': 0.695}, ignore_index=True)
fund_data_frame
Date | Value | Return | |
---|---|---|---|
0 | 2016-01-01 | 100.00 | 0.050 |
1 | 2016-02-01 | 110.25 | 0.050 |
2 | 2016-03-01 | 115.76 | 0.050 |
3 | 2016-04-01 | 121.55 | 0.050 |
4 | 2016-05-01 | 130.00 | 0.695 |
# Notice that this is a returned copy, not an in-place change
fund_data_frame[fund_data_frame.Value != 130]
Date | Value | Return | |
---|---|---|---|
0 | 2016-01-01 | 100.00 | 0.05 |
1 | 2016-02-01 | 110.25 | 0.05 |
2 | 2016-03-01 | 115.76 | 0.05 |
3 | 2016-04-01 | 121.55 | 0.05 |
# Alternatively, we can slice
fund_data_frame.ix[0:3]
Date | Value | Return | |
---|---|---|---|
0 | 2016-01-01 | 100.00 | 0.05 |
1 | 2016-02-01 | 110.25 | 0.05 |
2 | 2016-03-01 | 115.76 | 0.05 |
3 | 2016-04-01 | 121.55 | 0.05 |