如何基于数据框值和字典键将行添加到数据框

时间:2019-05-02 15:37:55

标签: python-3.x dataframe dictionary

我有一个defaultdict,数据框如下:

[('SERVER01', ['app01', 'app02']), ('SERVER02', ['app03','app04']), ('SERVER03', ['app05', 'app06','app07'])]

数据帧:

Date           Server          Satut          Risk
------------------------------------------------------
01/01/2019     SERVER01         Up             High
08/01/2019     SERVER02         Down           Low
01/02/2019     SERVER03         Up             High
08/02/2019     SERVER01         Down           High
10/02/2019     SERVER01         Up             Low

我想要输出:

Date           Server/app      Satut          Risk
------------------------------------------------------
01/01/2019     SERVER01         Up             High
01/01/2019     app01            Up             High
01/01/2019     app02            Up             High
08/01/2019     SERVER02         Down           Low
08/01/2019     app03            Down           Low
08/01/2019     app04            Down           Low
01/02/2019     SERVER03         Up             High
01/02/2019     app05            Up             High
01/02/2019     app06            Up             High
01/02/2019     app07            Up             High
08/02/2019     SERVER01         Down           High
08/02/2019     app01            Down           High
08/02/2019     app02            Down           High
10/02/2019     SERVER01         Up             Low
10/02/2019     app01            Up             Low
10/02/2019     app02            Up             Low

所以我想将键与列服务器的值链接,并复制该行,然后用应用替换服务器

1 个答案:

答案 0 :(得分:1)

这是您的默认字典:

d = defaultdict(list,
                {'SERVER01': ['app01', 'app02'],
                 'SERVER02': ['app03', 'app04'],
                 'SERVER03': ['app05', 'app06', 'app07']})

app_df = pd.DataFrame()
for k in d:
    temp_df = pd.DataFrame(d[k], 
                          index=[k] * len(d[k])).reset_index()
    temp_df.columns = ['Server', 'App']
    app_df = pd.concat([app_df, temp_df])

# This will give you the Server and App dataframe
    Server      App
0   SERVER01    app01
1   SERVER01    app02
0   SERVER02    app03
1   SERVER02    app04
0   SERVER03    app05
1   SERVER03    app06
2   SERVER03    app07

这段代码为您提供了所需的内容:

# Iterate over the keys of the defaultdict and get the row from the dataframe corresponds to that server
for k in d:
    r = df_raw.loc[df_raw['Server'] == k].to_dict(orient = 'records')[0]

    # Iterate over the apps of that server
    for app in d[k]:
        # make a copy of that row
        new_row = r.copy()
        # Update the Server key value with the app value
        new_row['Server'] = app
        # Convert it to a dataframe
        df_temp = pd.DataFrame.from_dict(new_row, orient='index').T
        # Append it to the main dataframe
        df_raw = df_raw.append(df_temp)

# Merge the two dataframes together
df_raw.merge(app_df, on='Server', how='inner')

输出此数据帧:

    Date    Server  Statu    Risk   App
0   1/1/19  SERVER01    Up   High   app01
1   1/1/19  SERVER01    Up   High   app02
2   8/1/19  SERVER02    Down Low    app03
3   8/1/19  SERVER02    Down Low    app04
4   1/2/19  SERVER03    Up   High   app05
5   1/2/19  SERVER03    Up   High   app06
6   1/2/19  SERVER03    Up   High   app07

也许有点矫kill过正,但这是我想到的第一件事,希望对您有所帮助!