Given the following data frame:
import pandas as pd
df=pd.DataFrame({'A':['a','b','c'],
'B':[[[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]]]})
df
A B
0 a [[1, 2], [3, 4], [5, 6]]
1 b [[1, 2], [3, 4], [5, 6]]
2 c [[1, 2], [3, 4], [5, 6]]
I'd like to create a new column ('C') containing the first value in each element of the tuple of column B like this:
A B C
0 a [[1, 2], [3, 4], [5, 6]] [1,3,5]
1 b [[1, 2], [3, 4], [5, 6]] [1,3,5]
2 c [[1, 2], [3, 4], [5, 6]] [1,3,5]
So far, I've tried:
df['C']=df['B'][0]
...but that only returns the first tuple ([1, 2]).
Thanks in advance!
This works for me -
df['C'] = df['B'].str[0]
df['C'] = df['B'].apply(lambda x: [y[0] for y in x])
try this:
df['C'] = df["B"].apply(lambda x : [y[0] for y in list(x)])
Related
When I need to add two 2D lists element wise, the approach I am using is
l1 = [[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]
l2 = [[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]
new = list(map(lambda e: [sum(x) for x in zip(*e)], zip(l1, l2)))
print(new)
output : [[2, 2, 2],
[4, 4, 4],
[6, 6, 6]]
This code is already difficult to read.
So how would I add two n dimensional lists element wise? Is there a pythonic way to do it or should I use numpy?
Given a nested list say:
a = [[1, 5, 100],
[2],
[2, 100]]
The desired result to be obtained is as follows:
[[1, 2, 5], [1, 2, 100], [5, 2, 100], [100, 2, 5]]
Here is my code, but it does not give the output as desired. I am unable to progress further:
arr = [[i] for i in a[0]]
def poss(j, arr, tmp):
for i in range(len(tmp)):
arr[i] = tmp[i] + [j]
print(arr)
for i in a[1:]:
tmp = [k for k in arr] # deepcopy of arr
for j in i:
poss(j, arr, tmp)
Output for above code:
[[1, 2], [5, 2], [100, 2]]
[[1, 2, 5], [5, 2, 5], [100, 2, 5]]
[[1, 2, 100], [5, 2, 100], [100, 2, 100]]
I also feel this code is inefficient on large data, is that so? I'm looking for a better code to get the result.
This problem can be solved by using itertools module of python.
The itertools.combinations() function returns all the possible subsets of the given set without repetition of elements.
import math
import itertools
a = [[1, 5, 100],
[2],
[2, 100]]
dimx = max([len(el) for el in a])
uniqueEls={}
for el in a:
for subel in el:
uniqueEls[subel] = uniqueEls.get(subel,0)
desiredArr= [list(x) for x in list(itertools.combinations(uniqueEls.keys(), dimx))]
print(desiredArr)
[[1, 5, 100], [1, 5, 2], [1, 100, 2], [5, 100, 2]]
i need to write a function in python that takes a matrix as an argument and divides each element of the matrix by 2 if the element is an even number (otherwise, does nothing).
i also need to use list comprehension for this.
as an example, if i have a matrix like m = [[5, 4], [2, 3], [6, 7]] output: [[5, 2], [1, 3], [3, 7]]
Thanks.
def f(matrix):
return [ [x//2 if x%2==0 else x for x in m ] for m in matrix]
print(f([[5, 4], [2, 3], [6, 7]]))
I started learning python recently. I am trying to sort a list of lists similar to this. However, I'm not able to find the correct method to do so please help.
Consider the list [[1,4], [3,3], [3,2] ,[1,2], [1,3], [2,3], [1,5]]
now, using
def keyFunc(j):
return j[0]
job = sorted(job, key=keyFunc, reverse=True)
I got it down to [[all 3s], [all 2s], [all 1s]]
However, now I want to further sort it so that the lists with common keys are in the order of descending values of their keys.
i.e. [[3,3], [3,2], [2,3], [1,5], [1,4], [1,3], [1,2]]
How does one do that in python?
Why do you use a wrong key function when not using a key function already does what you want?
>>> sorted(job, reverse=True)
[[3, 3], [3, 2], [2, 3], [1, 5], [1, 4], [1, 3], [1, 2]]
Or since you're assigning back to job anyway, you might want to do job.sort(reverse=True) instead of creating a new list.
You can change the keyFunc to be like
def keyFunc(j):
return j[0]*10+j[1]
or
ls = [[3, 3], [3, 2], [2, 3], [1, 5], [1, 4], [1, 3], [1, 2]]
sorted(ls, key=lambda x: x[0]*10+x[1], reverse=True)
That will sort both of the numbers as you described.
I think you can just negate the sorting keys to sort descending twice:
>>> lst = [[1,4], [3,3], [3,2] ,[1,2], [1,3], [2,3], [1,5]]
>>> sorted(lst, key=lambda x: (-x[0], -x[1]))
[[3, 3], [3, 2], [2, 3], [1, 5], [1, 4], [1, 3], [1, 2]]
(-x[0], -x[1]) will first sort by the first item, then if any ties occur sort on the second item, both in descending manner. We can make it descending by negating with the minus - sign.
But as suggested by #Heap Overflow, we don't need to do this because we can just pass reverse=True, and sorted() will naturally sort by the first item then the second in descending order. No need for a sorting key.
You can test this by running the following:
>>> sorted(lst, reverse=True)
[[3, 3], [3, 2], [2, 3], [1, 5], [1, 4], [1, 3], [1, 2]]
>>> sorted(lst, key=lambda x: (x[0], x[1]), reverse=True)
[[3, 3], [3, 2], [2, 3], [1, 5], [1, 4], [1, 3], [1, 2]]
Which both give the same results.
a1=[[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
i want the output to be:
a1=[[1, 2], [2, 3], [3, 4], [4, 5]]
I've tried removing it with a for loop, but it throws an error index out of range
You can use pop() if you want to remove by index (e.g. the fourth element):
In [1]: a1 = [[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
In [2]: a1.pop(4)
Out[2]: [3, 6]
In [3]: a1
Out[3]: [[1, 2], [2, 3], [2, 4], [3, 4], [4, 5]]
Or, you can remove by specifying the element:
In [4]: a1 = [[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
In [5]: a1.remove([3, 6])
In [6]: a1
Out[6]: [[1, 2], [2, 3], [2, 4], [3, 4], [4, 5]]
The answer is very simple just use the pop function.
https://www.geeksforgeeks.org/python-list-pop/
For your case it would be :
a1.pop(4)
you can loop over the Pop() function to remove multiple ones.