Loop through a dictionary of dataframesHow to merge two dictionaries in a single expression?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?Accessing the index in 'for' loops?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryLoop through an array in JavaScriptIterating over dictionaries using 'for' loopsHow to remove a key from a Python dictionary?
extending lines in 3d graph
Can my UK debt be collected because I have to return to US?
Received email from ISP saying one of my devices has malware
Punishment in pacifist society
Can「病気【びょうき】」be used as a 形容動詞【けいようどうし】 (na-adjective)?
Displaying Time in HH:MM Format
In Toy Story, are toys the only inanimate objects that become alive? And if so, why?
How do you manage to study and have a balance in your life at the same time?
Polarity of gas discharge tubes?
What happens if you just start drawing from the Deck of Many Things without declaring any number of cards?
Table alignment (make the content centre)
Quick Slitherlink Puzzles: KPK and 123
Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."
Divide Numbers by 0
Why didn't Thatcher give Hong Kong to Taiwan?
How to find better food in airports
Should we run PBKDF2 for every plaintext to be protected or should we run PBKDF2 only once?
Playing boules... IN SPACE!
If you can't target a creature without a clear path, does that mean Scrying fails unless you can already see the target?
Are there consequences for not filing a DMCA (any country)
Heuristic argument for the Riemann Hypothesis
Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code
Tiny image scraper for xkcd.com
An alternative to "two column" geometry proofs
Loop through a dictionary of dataframes
How to merge two dictionaries in a single expression?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?Accessing the index in 'for' loops?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryLoop through an array in JavaScriptIterating over dictionaries using 'for' loopsHow to remove a key from a Python dictionary?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a set of dataframes that represent scenarios of demand that I have put into a dictionary. I need to loop through each dataframe in the dictionary to reindex and resample etc. and the return to the dictionary. The below code works perfectly when I loop through a list of dataframes but I need to maintain the identity of each scenario, hence the dictionary.
This is the code that works with a list of dataframes:
demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high_demand_df, high_demand_df]
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dfs_datetime = []
for df in demand_dfs_list:
df.index = pd.to_datetime(df.index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dfs_datetime.append(df)
This is what I have tried in dictionary form:
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for df in demand_scenarios:
[df].index = pd.to_datetime([df].index, format='%Y')
[df] = [df].tshift(-92, 'D')
[df] = [df].resample('D').ffill()
[df] = [df].reindex(dates)
demand_dict[df] = df
FOLLOW UP QUESTION
I passed the above demand_dict dictionary into an xarray using the below:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates)
However my dataset looks like the following:
<xarray.Dataset>
Dimensions: (customers: 28, dim_0: 18262, dim_1: 28, time: 18262)
Coordinates:
* dim_0 (dim_0) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
* dim_1 (dim_1) object 'Customer_1' ... 'Customer_x'
* customers (customers) <U29 'Customer_1' ... 'Customer_x'
* time (time) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
Data variables:
low (dim_0, dim_1) float64 0.52 0.528 3.704 ... 7.744 0.92 64.47
medium_low (dim_0, dim_1) float64 0.585 0.594 4.167 ... 8.712 1.035 72.53
medium (dim_0, dim_1) float64 0.65 0.66 4.63 12.6 ... 9.68 1.15 80.59
medium_high (dim_0, dim_1) float64 0.715 0.726 5.093 ... 10.65 1.265 88.65
high (dim_0, dim_1) float64 0.78 0.792 5.556 ... 11.62 1.38 96.71
When I try and use the drop_dims function like so:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates).drop_dims(dim_0, dim_1)
I get the error:
AttributeError: 'Dataset' object has no attribute 'drop_dims'
Any idea why I am getting this error?
python pandas dictionary for-loop python-xarray
add a comment |
I have a set of dataframes that represent scenarios of demand that I have put into a dictionary. I need to loop through each dataframe in the dictionary to reindex and resample etc. and the return to the dictionary. The below code works perfectly when I loop through a list of dataframes but I need to maintain the identity of each scenario, hence the dictionary.
This is the code that works with a list of dataframes:
demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high_demand_df, high_demand_df]
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dfs_datetime = []
for df in demand_dfs_list:
df.index = pd.to_datetime(df.index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dfs_datetime.append(df)
This is what I have tried in dictionary form:
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for df in demand_scenarios:
[df].index = pd.to_datetime([df].index, format='%Y')
[df] = [df].tshift(-92, 'D')
[df] = [df].resample('D').ffill()
[df] = [df].reindex(dates)
demand_dict[df] = df
FOLLOW UP QUESTION
I passed the above demand_dict dictionary into an xarray using the below:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates)
However my dataset looks like the following:
<xarray.Dataset>
Dimensions: (customers: 28, dim_0: 18262, dim_1: 28, time: 18262)
Coordinates:
* dim_0 (dim_0) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
* dim_1 (dim_1) object 'Customer_1' ... 'Customer_x'
* customers (customers) <U29 'Customer_1' ... 'Customer_x'
* time (time) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
Data variables:
low (dim_0, dim_1) float64 0.52 0.528 3.704 ... 7.744 0.92 64.47
medium_low (dim_0, dim_1) float64 0.585 0.594 4.167 ... 8.712 1.035 72.53
medium (dim_0, dim_1) float64 0.65 0.66 4.63 12.6 ... 9.68 1.15 80.59
medium_high (dim_0, dim_1) float64 0.715 0.726 5.093 ... 10.65 1.265 88.65
high (dim_0, dim_1) float64 0.78 0.792 5.556 ... 11.62 1.38 96.71
When I try and use the drop_dims function like so:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates).drop_dims(dim_0, dim_1)
I get the error:
AttributeError: 'Dataset' object has no attribute 'drop_dims'
Any idea why I am getting this error?
python pandas dictionary for-loop python-xarray
add a comment |
I have a set of dataframes that represent scenarios of demand that I have put into a dictionary. I need to loop through each dataframe in the dictionary to reindex and resample etc. and the return to the dictionary. The below code works perfectly when I loop through a list of dataframes but I need to maintain the identity of each scenario, hence the dictionary.
This is the code that works with a list of dataframes:
demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high_demand_df, high_demand_df]
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dfs_datetime = []
for df in demand_dfs_list:
df.index = pd.to_datetime(df.index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dfs_datetime.append(df)
This is what I have tried in dictionary form:
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for df in demand_scenarios:
[df].index = pd.to_datetime([df].index, format='%Y')
[df] = [df].tshift(-92, 'D')
[df] = [df].resample('D').ffill()
[df] = [df].reindex(dates)
demand_dict[df] = df
FOLLOW UP QUESTION
I passed the above demand_dict dictionary into an xarray using the below:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates)
However my dataset looks like the following:
<xarray.Dataset>
Dimensions: (customers: 28, dim_0: 18262, dim_1: 28, time: 18262)
Coordinates:
* dim_0 (dim_0) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
* dim_1 (dim_1) object 'Customer_1' ... 'Customer_x'
* customers (customers) <U29 'Customer_1' ... 'Customer_x'
* time (time) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
Data variables:
low (dim_0, dim_1) float64 0.52 0.528 3.704 ... 7.744 0.92 64.47
medium_low (dim_0, dim_1) float64 0.585 0.594 4.167 ... 8.712 1.035 72.53
medium (dim_0, dim_1) float64 0.65 0.66 4.63 12.6 ... 9.68 1.15 80.59
medium_high (dim_0, dim_1) float64 0.715 0.726 5.093 ... 10.65 1.265 88.65
high (dim_0, dim_1) float64 0.78 0.792 5.556 ... 11.62 1.38 96.71
When I try and use the drop_dims function like so:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates).drop_dims(dim_0, dim_1)
I get the error:
AttributeError: 'Dataset' object has no attribute 'drop_dims'
Any idea why I am getting this error?
python pandas dictionary for-loop python-xarray
I have a set of dataframes that represent scenarios of demand that I have put into a dictionary. I need to loop through each dataframe in the dictionary to reindex and resample etc. and the return to the dictionary. The below code works perfectly when I loop through a list of dataframes but I need to maintain the identity of each scenario, hence the dictionary.
This is the code that works with a list of dataframes:
demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high_demand_df, high_demand_df]
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dfs_datetime = []
for df in demand_dfs_list:
df.index = pd.to_datetime(df.index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dfs_datetime.append(df)
This is what I have tried in dictionary form:
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for df in demand_scenarios:
[df].index = pd.to_datetime([df].index, format='%Y')
[df] = [df].tshift(-92, 'D')
[df] = [df].resample('D').ffill()
[df] = [df].reindex(dates)
demand_dict[df] = df
FOLLOW UP QUESTION
I passed the above demand_dict dictionary into an xarray using the below:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates)
However my dataset looks like the following:
<xarray.Dataset>
Dimensions: (customers: 28, dim_0: 18262, dim_1: 28, time: 18262)
Coordinates:
* dim_0 (dim_0) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
* dim_1 (dim_1) object 'Customer_1' ... 'Customer_x'
* customers (customers) <U29 'Customer_1' ... 'Customer_x'
* time (time) datetime64[ns] 2020-10-01 2020-10-02 ... 2070-09-30
Data variables:
low (dim_0, dim_1) float64 0.52 0.528 3.704 ... 7.744 0.92 64.47
medium_low (dim_0, dim_1) float64 0.585 0.594 4.167 ... 8.712 1.035 72.53
medium (dim_0, dim_1) float64 0.65 0.66 4.63 12.6 ... 9.68 1.15 80.59
medium_high (dim_0, dim_1) float64 0.715 0.726 5.093 ... 10.65 1.265 88.65
high (dim_0, dim_1) float64 0.78 0.792 5.556 ... 11.62 1.38 96.71
When I try and use the drop_dims function like so:
demand_xarray = xr.Dataset(demand_dict, coords = 'customers': customers, 'time': dates).drop_dims(dim_0, dim_1)
I get the error:
AttributeError: 'Dataset' object has no attribute 'drop_dims'
Any idea why I am getting this error?
python pandas dictionary for-loop python-xarray
python pandas dictionary for-loop python-xarray
edited Mar 28 at 17:01
AlexaB
asked Mar 28 at 1:22
AlexaBAlexaB
83 bronze badges
83 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for key, df in demand_scenarios.items():
df.index = pd.to_datetime([df].index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dict[key] = df
items()
return the key of the dictionary and the value
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
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%2f55388844%2floop-through-a-dictionary-of-dataframes%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
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for key, df in demand_scenarios.items():
df.index = pd.to_datetime([df].index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dict[key] = df
items()
return the key of the dictionary and the value
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
add a comment |
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for key, df in demand_scenarios.items():
df.index = pd.to_datetime([df].index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dict[key] = df
items()
return the key of the dictionary and the value
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
add a comment |
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for key, df in demand_scenarios.items():
df.index = pd.to_datetime([df].index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dict[key] = df
items()
return the key of the dictionary and the value
demand_scenarios = 'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dict =
for key, df in demand_scenarios.items():
df.index = pd.to_datetime([df].index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dict[key] = df
items()
return the key of the dictionary and the value
answered Mar 28 at 1:28
Mauricio CortazarMauricio Cortazar
2,5222 gold badges9 silver badges21 bronze badges
2,5222 gold badges9 silver badges21 bronze badges
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
add a comment |
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
Awesome, thank you. I got an error about my to_datetime line but turns out I actually didn't need to convert the index to datetime when using a dictionary... not sure I fully understand but it's working!
– AlexaB
Mar 28 at 1:46
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55388844%2floop-through-a-dictionary-of-dataframes%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