【发布时间】:2021-12-09 06:54:52
【问题描述】:
python 数据分析的新手,还是个菜鸟。
我有一个熊猫数据框列表 (+100),其中的变量保存在列表中。
然后我将变量以字符串格式保存在另一个列表中,以便在绘图时作为标识符添加到数据帧中。
我已经定义了一个函数来为以后的特征工程准备表格。
我想遍历每个数据框并将对应的字符串添加到名为“Strings”的列中
df = [df1, df2, df3]
strings = ['df1', 'df2', 'df3']
def mindex(df):
# remove time index and insert Strings column
df.reset_index(inplace=True)
df.insert(1, "Strings", "")
# iterate through each table adding the string values
for item in enumerate(df):
for item2 in strings:
df['Strings'] = item2
# the loop to cycle through all the dateframes using the function above
for i in df:
mindex(i)
当我使用上面的函数时,它只会将最后一个值填充到所有数据帧中。我想指出,所有数据帧都在同一日期范围内,因为我试图用它作为一种停止迭代而没有胜利的方法。
谁能指出我正确的方向!到目前为止,Google 还不是我的朋友
【问题讨论】:
-
我不完全理解目标。您是否尝试向每个 df 添加具有相应名称 df 的列?即 df1 应该有一个名为“Strings”的列,它将在所有行中采用值“df1”?
-
是的。 df1 将有一列,其中所有行都显示为 'df1'、df2 = 'df2' 等
标签: python pandas list dataframe