我想使用值s作為每個 DataFrame 的名稱來創建n 個DataFrame,但我只能創建一個完整的 DataFrame 串列。可以在其中的每個 DataFrame 中更改此串列嗎?
#estacao has something like [ABc,dfg,hil,...,xyz], and this should be the name of each DataFrame
estacao = dados.Station.unique()
for s,i in zip(estacao,range(126)):
estacao[i] = dados.groupby('Station').get_group(s)
uj5u.com熱心網友回復:
我會在這里使用字典。然后你可以命名鍵s和值可以是對應于該組的資料框:
groups = dados.Station.unique()
groupby_ = datos.groupby('Station')
dataframes = {s: groupby_.get_group(s) for s in groups}
然后通過名稱呼叫每一個就像這樣簡單:
group_df = dataframes['group_name']
uj5u.com熱心網友回復:
如果您真的需要s創建以(我group在下面的示例中命名)命名的DataFrame ,使用exec是解決方案。
groups = dados.Station.unique()
groupby_ = dados.groupby('Station')
for group in groups:
exec(f"{group} = groupby_.get_group('{group:s}')")
警告
請參閱此答案以了解為什么使用exec和eval命令并不總是可取的。
為什么要避免 exec() 和 eval()?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/458754.html
下一篇:Pandas:將資料框重構為列值
