Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a list from another lists Pandas

I have the following dataframe.

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]

from this list I would like to pick the second value from the list values and get the following list.

new_list = [4364, 791, 2847, 1296]

Can any one help on this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What you are describing are just lists not pandas DataFrames. In order to get the second element, the easiest solution is probably to use list comprehension:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
new_list = [x for _, x in my_list]
# or
new_list = [x[1] for x in my_list]

If you are indeed working with pandas, you can use [] to get desired column:

df = pandas.DataFrame([(343, 4364), (221, 791), (221, 2847), (21, 1296)])
col = df[1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading