I have a sample dataframe:
Id Age Name
0 0110 23 Max
1 2009 56 Stan
2 1167 25 Joy
3 8878 44 Lee
at present for the input dataframe I'm using
dfnew = df.drop(df.columns.difference(['Id']),1)
to drop unwanted column(i.e Id) in dataframe and proceeding ahead. Which is correct but there are two dataframes.
is there anyway we can optimise it?
while converting the dataframe to a list can we have only age and name in the list and ignore Id like lst = [[23,Max],[56,stan]....]
please assist on this. Thank you.
CodePudding user response:
You can use drop
the unwanted column, convert to a numpy array and convert to list. This is faster than apply(list, axis=1)
.
lst = df.drop(columns='Id').to_numpy().tolist()
Output:
[[23, 'Max'], [56, 'Stan'], [25, 'Joy'], [44, 'Lee']]