adding values to a sub column with pandas in python
How can I add a subcolumn in a dataframe ?
here is what I have so far...
start_date = datetime.today() - timedelta(days=252)
end_date = datetime.today()
stock_list = ['TSLA', 'XOM', 'AAPL']
data = iex.stocks.get_historical_data(stock_list, start_date, end_date, filter=['close'], output_format='pandas')
print(data.head())
data['TSLA']['sma50'] = data['TSLA']['close'].rolling(50).mean()
I'm getting this error:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
this is the data:
TSLA XOM AAPL
open high low close volume open high low close volume open high low close volume
date
2018-04-25 283.50 285.16 277.25 280.690 4013574 75.8561 77.2335 75.1625 77.1171 13865599 160.8679 163.6377 160.6602 161.8868 28382084
2018-04-26 278.75 285.79 276.50 285.480 4356013 77.2141 78.4751 77.1462 78.4363 14554442 162.3518 163.9444 161.6098 162.4507 27963014
2018-04-27 285.37 294.47 283.83 294.075 4364626 74.6435 76.2926 74.1876 75.4584 16346833 162.2330 162.5595 158.8994 160.5711 35655839
2018-04-30 293.61 298.73 292.50 293.900 4228172 75.5748 76.2053 75.4099 75.4196 15028835 160.3834 165.4579 160.0963 163.4795 42427424
2018-05-01 293.51 300.82 293.22 299.920 4625603 74.9443 74.9443 73.7026 74.6435 16231035 164.6173 167.3770 163.4894 167.2781 53569376
Desire output
TSLA XOM AAPL
open high low close volume sma50 open high low close volume sma50 open high low close volume sma50
python pandas
add a comment |
How can I add a subcolumn in a dataframe ?
here is what I have so far...
start_date = datetime.today() - timedelta(days=252)
end_date = datetime.today()
stock_list = ['TSLA', 'XOM', 'AAPL']
data = iex.stocks.get_historical_data(stock_list, start_date, end_date, filter=['close'], output_format='pandas')
print(data.head())
data['TSLA']['sma50'] = data['TSLA']['close'].rolling(50).mean()
I'm getting this error:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
this is the data:
TSLA XOM AAPL
open high low close volume open high low close volume open high low close volume
date
2018-04-25 283.50 285.16 277.25 280.690 4013574 75.8561 77.2335 75.1625 77.1171 13865599 160.8679 163.6377 160.6602 161.8868 28382084
2018-04-26 278.75 285.79 276.50 285.480 4356013 77.2141 78.4751 77.1462 78.4363 14554442 162.3518 163.9444 161.6098 162.4507 27963014
2018-04-27 285.37 294.47 283.83 294.075 4364626 74.6435 76.2926 74.1876 75.4584 16346833 162.2330 162.5595 158.8994 160.5711 35655839
2018-04-30 293.61 298.73 292.50 293.900 4228172 75.5748 76.2053 75.4099 75.4196 15028835 160.3834 165.4579 160.0963 163.4795 42427424
2018-05-01 293.51 300.82 293.22 299.920 4625603 74.9443 74.9443 73.7026 74.6435 16231035 164.6173 167.3770 163.4894 167.2781 53569376
Desire output
TSLA XOM AAPL
open high low close volume sma50 open high low close volume sma50 open high low close volume sma50
python pandas
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
1
Actual data-frame?
– U9-Forward
Jan 2 at 2:28
add a comment |
How can I add a subcolumn in a dataframe ?
here is what I have so far...
start_date = datetime.today() - timedelta(days=252)
end_date = datetime.today()
stock_list = ['TSLA', 'XOM', 'AAPL']
data = iex.stocks.get_historical_data(stock_list, start_date, end_date, filter=['close'], output_format='pandas')
print(data.head())
data['TSLA']['sma50'] = data['TSLA']['close'].rolling(50).mean()
I'm getting this error:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
this is the data:
TSLA XOM AAPL
open high low close volume open high low close volume open high low close volume
date
2018-04-25 283.50 285.16 277.25 280.690 4013574 75.8561 77.2335 75.1625 77.1171 13865599 160.8679 163.6377 160.6602 161.8868 28382084
2018-04-26 278.75 285.79 276.50 285.480 4356013 77.2141 78.4751 77.1462 78.4363 14554442 162.3518 163.9444 161.6098 162.4507 27963014
2018-04-27 285.37 294.47 283.83 294.075 4364626 74.6435 76.2926 74.1876 75.4584 16346833 162.2330 162.5595 158.8994 160.5711 35655839
2018-04-30 293.61 298.73 292.50 293.900 4228172 75.5748 76.2053 75.4099 75.4196 15028835 160.3834 165.4579 160.0963 163.4795 42427424
2018-05-01 293.51 300.82 293.22 299.920 4625603 74.9443 74.9443 73.7026 74.6435 16231035 164.6173 167.3770 163.4894 167.2781 53569376
Desire output
TSLA XOM AAPL
open high low close volume sma50 open high low close volume sma50 open high low close volume sma50
python pandas
How can I add a subcolumn in a dataframe ?
here is what I have so far...
start_date = datetime.today() - timedelta(days=252)
end_date = datetime.today()
stock_list = ['TSLA', 'XOM', 'AAPL']
data = iex.stocks.get_historical_data(stock_list, start_date, end_date, filter=['close'], output_format='pandas')
print(data.head())
data['TSLA']['sma50'] = data['TSLA']['close'].rolling(50).mean()
I'm getting this error:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
this is the data:
TSLA XOM AAPL
open high low close volume open high low close volume open high low close volume
date
2018-04-25 283.50 285.16 277.25 280.690 4013574 75.8561 77.2335 75.1625 77.1171 13865599 160.8679 163.6377 160.6602 161.8868 28382084
2018-04-26 278.75 285.79 276.50 285.480 4356013 77.2141 78.4751 77.1462 78.4363 14554442 162.3518 163.9444 161.6098 162.4507 27963014
2018-04-27 285.37 294.47 283.83 294.075 4364626 74.6435 76.2926 74.1876 75.4584 16346833 162.2330 162.5595 158.8994 160.5711 35655839
2018-04-30 293.61 298.73 292.50 293.900 4228172 75.5748 76.2053 75.4099 75.4196 15028835 160.3834 165.4579 160.0963 163.4795 42427424
2018-05-01 293.51 300.82 293.22 299.920 4625603 74.9443 74.9443 73.7026 74.6435 16231035 164.6173 167.3770 163.4894 167.2781 53569376
Desire output
TSLA XOM AAPL
open high low close volume sma50 open high low close volume sma50 open high low close volume sma50
python pandas
python pandas
edited Jan 2 at 2:54
Fede
asked Jan 2 at 2:20
FedeFede
71131533
71131533
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
1
Actual data-frame?
– U9-Forward
Jan 2 at 2:28
add a comment |
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
1
Actual data-frame?
– U9-Forward
Jan 2 at 2:28
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
1
1
Actual data-frame?
– U9-Forward
Jan 2 at 2:28
Actual data-frame?
– U9-Forward
Jan 2 at 2:28
add a comment |
1 Answer
1
active
oldest
votes
Check out the 'Hierarchical indexing (MultiIndex)' functions of pandas.
You should create an array of tuples like this one:
index = [('TSLA', 'open'), ('TSLA', 'high'),
('TSLA', 'low'), ('TSLA', 'close'),('XOM', 'open'), ('XOM', 'high'),
('XOM', 'low'), ('XOM', 'close')] #etc for all of youe stcks....
and the a new array of values for each group in the tuple:
values = [283.50, 285.16] #etc for all values...
Then you should call th Multiindex function of pandas like this:
index = pd.MultiIndex.from_tuples(index)
Then you get the dataframe you want by calling:
data = pd.Dataframe(data=values,index=index)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000521%2fadding-values-to-a-sub-column-with-pandas-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check out the 'Hierarchical indexing (MultiIndex)' functions of pandas.
You should create an array of tuples like this one:
index = [('TSLA', 'open'), ('TSLA', 'high'),
('TSLA', 'low'), ('TSLA', 'close'),('XOM', 'open'), ('XOM', 'high'),
('XOM', 'low'), ('XOM', 'close')] #etc for all of youe stcks....
and the a new array of values for each group in the tuple:
values = [283.50, 285.16] #etc for all values...
Then you should call th Multiindex function of pandas like this:
index = pd.MultiIndex.from_tuples(index)
Then you get the dataframe you want by calling:
data = pd.Dataframe(data=values,index=index)
add a comment |
Check out the 'Hierarchical indexing (MultiIndex)' functions of pandas.
You should create an array of tuples like this one:
index = [('TSLA', 'open'), ('TSLA', 'high'),
('TSLA', 'low'), ('TSLA', 'close'),('XOM', 'open'), ('XOM', 'high'),
('XOM', 'low'), ('XOM', 'close')] #etc for all of youe stcks....
and the a new array of values for each group in the tuple:
values = [283.50, 285.16] #etc for all values...
Then you should call th Multiindex function of pandas like this:
index = pd.MultiIndex.from_tuples(index)
Then you get the dataframe you want by calling:
data = pd.Dataframe(data=values,index=index)
add a comment |
Check out the 'Hierarchical indexing (MultiIndex)' functions of pandas.
You should create an array of tuples like this one:
index = [('TSLA', 'open'), ('TSLA', 'high'),
('TSLA', 'low'), ('TSLA', 'close'),('XOM', 'open'), ('XOM', 'high'),
('XOM', 'low'), ('XOM', 'close')] #etc for all of youe stcks....
and the a new array of values for each group in the tuple:
values = [283.50, 285.16] #etc for all values...
Then you should call th Multiindex function of pandas like this:
index = pd.MultiIndex.from_tuples(index)
Then you get the dataframe you want by calling:
data = pd.Dataframe(data=values,index=index)
Check out the 'Hierarchical indexing (MultiIndex)' functions of pandas.
You should create an array of tuples like this one:
index = [('TSLA', 'open'), ('TSLA', 'high'),
('TSLA', 'low'), ('TSLA', 'close'),('XOM', 'open'), ('XOM', 'high'),
('XOM', 'low'), ('XOM', 'close')] #etc for all of youe stcks....
and the a new array of values for each group in the tuple:
values = [283.50, 285.16] #etc for all values...
Then you should call th Multiindex function of pandas like this:
index = pd.MultiIndex.from_tuples(index)
Then you get the dataframe you want by calling:
data = pd.Dataframe(data=values,index=index)
answered Jan 2 at 10:42
PetrosPetros
3318
3318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000521%2fadding-values-to-a-sub-column-with-pandas-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
desired output? also, please do not ever post code or data-frame as images
– U9-Forward
Jan 2 at 2:22
in each column ( TSLA, XOM, AAPL) I want to add a subcolumn ( sma50 ) putput: (open, high, low, close, volume, sma50) for each column
– Fede
Jan 2 at 2:27
1
Actual data-frame?
– U9-Forward
Jan 2 at 2:28