0

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.

1
  • 3
    df[["Age", "Name"]].apply(list, axis=1).tolist() OR df.drop("Id", axis=1).apply(list, axis=1).tolist()? Commented Dec 22, 2021 at 14:25

1 Answer 1

1

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']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.