Home > Software engineering >  Creating a list from another lists Pandas
Creating a list from another lists Pandas

Time:01-06

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?

CodePudding user response:

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]

CodePudding user response:

use this snippets:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
res=[]
for i,x in my_list:
    res.append(x)

print(res)

Output:

[4364, 791, 2847, 1296]

CodePudding user response:

You can do it with list comprehension:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
new_list = [i[1] for i in my_list]
print(new_list)
  •  Tags:  
  • Related