python - Unpacking list of lists generated by a zip into one list -
i again manipulating dataframes. here concatenate multiple dataframe using row common reference. want reorder columns "pairing" first 1 columns of each df together, , on. sake of data readability
here code:
df_list=[df_1,df_2,df_3] return_df=pd.concat(df_list,axis=1, join='outer') dfcolumns_list=[df_1.columns,df_2.columns,df_3.columns] print (return_df.columns) print(dfcolumns_list) list_columns=np.array(list(zip(*dfcolumns_list))).reshape(1,-1)[0] print (list_columns) list_columns=np.array([x x in zip(*dfcolumns_list)]).reshape(1,-1)[0] print (list_columns) return_df=return_df[list_columns]
my question related to:
list_columns=np.array(list(zip(*dfcolumns_list))).reshape(1,-1)[0]
or alternatively
list_columns=np.array([x x in zip(*dfcolumns_list)]).reshape(1,-1)[0]
it takes list of indexes, unpacks in zip, takes first element of each column index, outputs tuple/sublist contained in list, transforms array ,then reshapes rid of sublists cause
return_df=return_df[list_columns]
to break. @ last, call index 0 [0]
allows retrieve final list np.array (which need reshape).
my question is: there nothing less ugly that? zip
, similar functions, hate have no simple mean/trick unpack generated tuples/sublist reordering purposes.
(it came mind while redacting maybe df differently, give points that, main question still how doing more elegantly/with more pythonic syntax. [0]
in end dirtiest of all...
you may zip column lists , flatten list of lists
list_columns = [ col cols in zip( *dfcolumns_list ) col in cols ]
Comments
Post a Comment