简体   繁体   中英

Pandas fill column with row value

I have a dataframe that look like this.

key            values
Interface       InterfaceA
State           Up
Line Status     Up
ID              9000
Interface       InterfaceB
State           Down
Line Status     Down
ID              9001

And I would like to transform it to become like this

Interface        State        Line Status       ID
InterfaceA        Up             Up             9000
InterfaceB        Down           Down           9001

I've try to use loc to insert the column by column, but when it reaches 2nd columns

ValueError: cannot reindex from a duplicate axis

the above error appears.

final_df['Interface'] = commands_df.loc[commands_df['key'].str.contains('Interface'), 'values']
final_df['State'] = commands_df.loc[commands_df['key'].str.contains('State'), 'values'] <-- Error starts here

ValueError: cannot reindex from a duplicate axis
df = df.assign(Interface=df[df['key'] == 'Interface']['values']).ffill()
print(df.pivot(index='Interface', columns='key', values='values').drop(columns='Interface'))

Prints:

key           ID Line Status State
Interface                         
InterfaceA  9000          Up    Up
InterfaceB  9001        Down  Down

Here is a possible solution-

import pandas as pd

df = pd.DataFrame(data=['InterfaceA', 'Up', 'Up', 9000, 'InterfaceB', 'Down', 'Down', 9001],
                  index=['Interface', 'State', 'Line Status', 'ID', 'Interface', 'State', 'Line Status', 'ID'])

df = df.T
print(df.groupby(df.columns.values, axis=1).agg(lambda x: x.values.tolist()).sum().apply(pd.Series).T)
     ID   Interface Line Status State
0  9000  InterfaceA          Up    Up
1  9001  InterfaceB        Down  Down

credits to this answer

A simple set_index with cumcount and unstack

df_final = df.set_index([df.groupby('key').cumcount(),'key'])['values'].unstack()

Out[423]:
key    ID   Interface Line-Status State
0    9000  InterfaceA          Up    Up
1    9001  InterfaceB        Down  Down

Another method is using pd.crosstab

df_final = pd.crosstab(df.groupby('key')['values'].cumcount(), 
                       df['key'], 
                       df['values'], aggfunc='first')

Out[424]:
key      ID   Interface Line-Status State
row_0
0      9000  InterfaceA          Up    Up
1      9001  InterfaceB        Down  Down

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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