Create One Row With Values From Table With Data In Diagonal Only
I have the following table in pandas- >>>index1 index2 index3 index4 index5 index6 index7 0 sig null null null null null null 1 nul
Solution 1:
You can try np.diagonal
which return diagonal
elemnts:
d = pd.DataFrame([np.diagonal(df)], columns=df.columns)
index1 index2 index3 index4 index5 index6 index7
0 sig sig sig sig no sig no sig sig
Solution 2:
IIUC, use pandas.DataFrame.bfill
:
new_df = df.bfill().head(1)
print(new_df)
Output:
index1 index2 index3 index4 index5 index6 index7
0 sig sig sig sig no sig no sig sig
Solution 3:
Another alternative with stack
, and droplevel
;
df.stack().droplevel(0).to_frame().T
index1 index2 index3 index4 index5 index6 index7
0 sig sig sig sig no sig no sig sig
Solution 4:
A pandas solution of np.diagonal
is df.lookup
df_diag = pd.DataFrame([df.lookup(df.index, df.columns)], columns=df.columns)
Out[47]:
index1 index2 index3 index4 index5 index6 index7
0 sig sig sig sig no-sig no-sig sig
Post a Comment for "Create One Row With Values From Table With Data In Diagonal Only"