是否有一种更优雅的方法来初始化空数据框

发布于 2025-02-11 20:10:22 字数 303 浏览 6 评论 0原文

isd = pd.DataFrame()
ind = pd.DataFrame()
exd = pd.DataFrame()
psd = pd.DataFrame()
visd = pd.DataFrame()
vind = pd.DataFrame()
vexd = pd.DataFrame()
sd = pd.DataFrame()
ise = pd.DataFrame()
idb = pd.DataFrame()
mdd = pd.DataFrame()
add = pd.DataFrame()

是否有其他方法可以使其优雅,更快?

isd = pd.DataFrame()
ind = pd.DataFrame()
exd = pd.DataFrame()
psd = pd.DataFrame()
visd = pd.DataFrame()
vind = pd.DataFrame()
vexd = pd.DataFrame()
sd = pd.DataFrame()
ise = pd.DataFrame()
idb = pd.DataFrame()
mdd = pd.DataFrame()
add = pd.DataFrame()

Is there any alternate way to make it elegant and faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

电影里的梦 2025-02-18 20:10:22

使用数据框的字典,尤其是某些数据框架的代码将共享一些相似之处。这将允许使用循环或功能进行一些操作:

dct = {n: pd.DataFrame() for n in ['isd', 'ind', 'exd']} 

Use a dictionary of dataframes, especially of the code for some data frames is going to share some similarities. This will allows doing some operations using loops or functions:

dct = {n: pd.DataFrame() for n in ['isd', 'ind', 'exd']} 
爱人如己 2025-02-18 20:10:22

如果您想避免需要数字索引每个dataFrames,但宁愿能够直接以其名称访问它们:

import pandas as pd

table_names = ['df1', 'df2', 'df3']
for name in table_names:
  exec('%s = pd.DataFrame()' % name, locals(), locals())

print(df1)

此方法使用 exec ,基本上像Python代码一样运行字符串。我只是将每个预定的名称格式化为前面的字符串。

If you want to avoid needing to numerically index each of the DataFrames, but would rather be able to access them directly by their name:

import pandas as pd

table_names = ['df1', 'df2', 'df3']
for name in table_names:
  exec('%s = pd.DataFrame()' % name, locals(), locals())

print(df1)

This approach uses exec, which essentially runs a string as if it were python code. I'm just formatting each of the predetermined names into the string in a for-loop.

沩ん囻菔务 2025-02-18 20:10:22

您可以做这样的事情:

dfs = ['isd', 'ind', 'exd']
df_list = [pd.DataFrame() for df in dfs ]

You can do something like this:

dfs = ['isd', 'ind', 'exd']
df_list = [pd.DataFrame() for df in dfs ]
穿透光 2025-02-18 20:10:22

我认为你可以这样去

import pandas as pd

a, b, c, d = [pd.DataFrame()]*4

I think you can go this way

import pandas as pd

a, b, c, d = [pd.DataFrame()]*4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文