简体   繁体   English

使用for循环附加到Pandas中的空数据框

[英]Appending to an empty data frame in Pandas using a for loop

I am writing because I am having an issue with a for loop which fills a dataframe when it is empty. 我写这篇文章是因为我有一个for循环的问题,该循环会在数据框为空时填充该数据框。 Unfortunately, the posts Filling empty python dataframe using loops , Appending to an empty data frame in Pandas? 不幸的是,这些帖子使用循环填充了空的python 数据框,并在Pandas中附加到了一个空的数据框? , Creating an empty Pandas DataFrame, then filling it? 创建一个空的Pandas DataFrame,然后填充它? did not help me to solve it. 没帮我解决。

My attempt aims, first, at finding the empty dataframes in the list "listDataframe" and then, wants to fill them with some chosen columns. 我的尝试首先是要在列表“ listDataframe”中找到空的数据框,然后要用一些选定的列填充它们。 I believe my code is clearer than my explanation. 我相信我的代码比我的解释更清楚。 What I can't do is to save the new dataframe using its original name. 我不能做的就是使用其原始名称保存新数据框。 Here my attempt: 这是我的尝试:

for k,j in zip(listOwner,listDataframe):
for y in j:
    if y.empty:
        data = pd.DataFrame({"Event Date": list_test_2, "Site Group Name" : k, "Impressions" : 0})
        y = pd.concat([data,y])
        #y = y.append(data)

where "listOwner", "listDataframe" and "list_test_2" are, respectively, given by: 其中“ listOwner”,“ listDataframe”和“ list_test_2”分别由下式给出:

listOwner = ['OWNER ONE', 'OWNER TWO', 'OWNER THREE', 'OWNER FOUR']
listDataframe = [df_a,df_b,df_c,df_d]

with

df_a = [df_ap_1, df_di_1, df_er_diret_1, df_er_s_1]
df_b = [df_ap_2, df_di_2, df_er_diret_2, df_er_s_2]
df_c = [df_ap_3, df_di_3, df_er_diret_3, df_er_s_3]
df_d = [df_ap_4, df_di_4, df_er_diret_4, df_er_s_4]

and

list_test_2 = []
for i in range(1,8):
    f = (datetime.today() - timedelta(days=i)).date()
    list_test_2.append(datetime.combine(f, datetime.min.time()))

The empty dataframe were df_ap_1 and df_ap_3. 空数据帧为df_ap_1和df_ap_3。 After running the above lines (using both concat and append) if I call these two dataframes they are still empty. 在运行完以上几行(使用concat和append)之后,如果我调用这两个数据帧,它们仍然为空。 Any idea why that happens and how to overcome this issue? 任何想法为什么会发生以及如何克服这个问题?

UPDATE UPDATE

In order to avoid both append and concat, I tried to use the coming attempt (again with no success). 为了避免append和concat,我尝试使用即将来临的尝试(同样没有成功)。

for k,j in zip(listOwner,listDataframe):
    for y in j:
        if y.empty:
            y = pd.DataFrame({"Event Date": list_test_2, "Site Group Name" : k, "Impressions" : 0})

The two desired result should be: 两个理想的结果应该是:

在此处输入图片说明

where the first dataframe should be called df_ap_1 while the second one df_ap_3 . 其中第一个数据帧应称为df_ap_1而第二个数据帧应称为df_ap_3

Thanks in advance. 提前致谢。

Drigo Drigo

Here's a way to do it: 这是一种方法:

import pandas as pd

columns = ['Event Date', 'Site Group Name', 'Impressions']
df_ap_1 = pd.DataFrame(columns=columns) #empty dataframe
df_di_1 = pd.DataFrame(columns=columns) #empty dataframe
df_ap_2 = pd.DataFrame({'Event Date':[1], 'Site Group Name':[2], 'Impressions': [3]}) #non-empty dataframe
df_di_2 = pd.DataFrame(columns=columns) #empty dataframe

df_a = [df_ap_1, df_di_1]
df_b = [df_ap_2, df_di_2]
listDataframe = [df_a,df_b]

list_test_2 = 'foo'
listOwner = ['OWNER ONE', 'OWNER TWO']

def appendOwner(df, owner, list_test_2):
    #appends a row to a dataframe for each row in listOwner
    new_row = {'Event Date': list_test_2,
               'Site Group Name': owner,
               'Impressions': 0,
               }
    df.loc[len(df)] = new_row

for owner, dfList in zip(listOwner, listDataframe):
    for df in dfList:
        if df.empty:
            appendOwner(df, owner, list_test_2)

print(listDataframe)

You can use the appendOwner function to append the rows from listOwner to an empty dataframe. 您可以使用appendOwner函数将appendOwner的行追加到一个空的数据框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM