How can I separate tuples into columns in a Pandas DataFrame? - python

I have these values in dataset in a pandas dataframe column
col1
[[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]]
[[13,14],[15,16],[17,18],[19,20],[21,22],[23,24]]
I want to get 6 elements as list in new columns as rows.
This is the columns that I want to get.
col2 col3
[1,3,5,7,9,11] [2,4,6,8,10,12]
[13,15,17,19,21,23] [14,16,18,20,22,24]

You can use a list comprehension and the DataFrame constructor:
df[['col2', 'col3']] = pd.DataFrame([list(map(list, zip(*l))) for l in df['col1']])
Another approach with numpy:
a = np.dstack(df['col1'].to_numpy())
df['col2'] = a[:,0].T.tolist()
df['col3'] = a[:,1].T.tolist()
Output:
col1 col2 col3
0 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]] [1, 3, 5, 7, 9, 11] [2, 4, 6, 8, 10, 12]
1 [[13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]] [13, 15, 17, 19, 21, 23] [14, 16, 18, 20, 22, 24]

Related

Find a value of a list in list and write a comment like duplicated python

list = [[2, 3, 4], [6, 7, 4], [10, 11, 12], [14, 15, 16], [18, 19, 12]]
I wont give a variable to code it will check the lists fourth value and if its duplicated in another list code will append a "found" string
list = [[2, 3, 4, "found"], [6, 7, 4, "found"], [10, 11, 12, "found"], [14, 15, 16], [18, 19, 12, "found"]]
I tried a lot but couldn't find any solutions.
listm = [[1, 2, 3, 4], [5, 6, 7, 4], [9, 10, 11, 12], [13, 14, 15, 16]]
c = 4
for i, alt_list in enumerate(listm):
if c in alt_list:
listm[i].append("found")
print(listm)
Use an auxiliary dict to count occurrences of the last value of inner lists:
lst = [[2, 3, 4], [6, 7, 4], [10, 11, 12], [14, 15, 16], [18, 19, 12]]
counts = {}
for sub_l in lst:
counts[sub_l[-1]] = 1 if sub_l[-1] not in counts else sub_l[-1] + 1
lst = [l + ['found'] if counts[l[-1]] > 1 else l for l in lst]
print(lst)
[[2, 3, 4, 'found'], [6, 7, 4, 'found'], [10, 11, 12, 'found'], [14, 15, 16], [18, 19, 12, 'found']]
Use an auxiliary dict to count occurrences of the last value of inner lists:
lst = [[2, 3, 4], [6, 7, 4], [10, 11, 12], [14, 15, 16], [18, 19, 12]]
counts = {}
for sub_l in lst:
counts[sub_l[-1]] = 1 if sub_l[-1] not in counts else sub_l[-1] + 1
lst = [l + ['found'] if counts[l[-1]] > 1 else l for l in lst]
print(lst)
Output:
[[2, 3, 4, 'found'], [6, 7, 4, 'found'], [10, 11, 12, 'found'], [14, 15, 16], [18, 19, 12, 'found']]
Here is a counter-based approach:
from collections import Counter
lists = [[2, 3, 4], [6, 7, 4], [10, 11, 12], [14, 15, 16], [18, 19, 12]]
final_counts = Counter(sublist[-1] for sublist in lists)
for sublist in lists:
if final_counts[sublist[-1]] > 1:
sublist.append("found")
print(lists)
Output:
[[2, 3, 4, 'found'], [6, 7, 4, 'found'], [10, 11, 12, 'found'], [14, 15, 16], [18, 19, 12, 'found']]

Creating 2D list from 3D list in Python [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 11 months ago.
Assume I have a 3D list as following:
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
I want to convert it to:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
I do not want to get lost inside of the for loops, is there a way this can be easily implemented?
Thanks in advance.
Try summing the list with []:
sum(your_3d_list, [])
Attempt it Online!
You can use numpy module and reshape function:
import numpy as np
myList = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
array = np.array(myList)
array.reshape(9,3)
Output
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 22, 23],
[24, 25, 26]])
You can use assert in order to make sure that this is the expected array:
expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
array = np.array(myList)
assert array.reshape(9,3).tolist() == expected
which works fine!
Note that, array.reshape(9,3) returns a numpy array and not a list. If you want to have the expected array as a list, you can use array.reshape(9,3).tolist()
array.reshape(9,3).tolist()
Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
Possible solution is the following:
lst = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
result = [item for subitem in lst for item in subitem]
print(result)
Prints
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
​
You can use
import itertools
library, which is a standard python library.
use it like this:
import itertools
array = itertools.chain.from_iterable(array)
array = list(array)
the output will be:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
You can loop through each 2d list in the 3d list, then loop through each 1d list in the 2d list
_3d_list = [[[1, 2, 3], [3, 2, 1], [1, 3, 5]], [[4, 5, 6], [6, 5, 4], [4, 6, 8]]]
final_list = []
for _2d_list in _3d_list:
for _1d_list in _2d_list:
final_list.append(_1d_list)
print(final_list)
Output:
[[1, 2, 3], [3, 2, 1], [1, 3, 5], [4, 5, 6], [6, 5, 4], [4, 6, 8]]

An easy way to create a torch tensor from multiple elements of tuple through concatenate

Inputs
I have list as follow
r1 = [([[[1, 2, 3], [1, 2, 3]],
[[4, 5, 6], [4, 5, 6]]],
[[[7, 8], [7, 8]],
[[9, 10], [9, 10]]]),
([[[11, 12, 13], [11, 12, 13]],
[[14, 15, 16], [14, 15, 16]]],
[[[17, 18], [17, 18]],
[[19, 20], [19, 20]]])]
I'm going to make 2 torch tensors from the input above.
my desired output is as follow
output
output =
[tensor([[[ 1, 2, 3],
[ 1, 2, 3]],
[[ 4, 5, 6],
[ 4, 5, 6]],
[[11, 12, 13],
[11, 12, 13]],
[[14, 15, 16],
[14, 15, 16]]]),
tensor([[[ 7, 8],
[ 7, 8]],
[[ 9, 10],
[ 9, 10]],
[[17, 18],
[17, 18]],
[[19, 20],
[19, 20]]])]
My code is as follows.
output = []
for i in range(len(r1[0])):
templates = []
for j in range(len(r1)):
templates.append(torch.tensor(r1[j][i]))
template = torch.cat(templates)
output.append(template)
Is there a simpler or easier way to get the result I want?
This will do:
output = [torch.Tensor([*a, *b]) for a, b in zip(*r1)]
It concatenates the corresponding items of the two list first then create the Tensor

Extract maximum list based on a common element in lists in a list

I am wondering if it is possible to extract maximum list based on a common element in lists in a list.
What I mean is that suppose we have the following list of lists:
list = [['p_1', [3, 4, 5, 6], 6.2],
['p_2', [3, 4, 5, 6], 8.4],
['p_3', [3, 7, 8, 9],9.1],
['p_4', [3, 4, 5, 6],8.2]]
My plan is to compare second element in each list with second element in other list and then if the second element is the same, choose the list with the highest third element. For example, the second elements of the first, second and forth lists are the same (i.e [3,4,5,6]). Now since 6.2 < 8.2 < 8.4 the second list will be chosen. In addition, since the second element of the third list is unique this list should also be chosen. In fact, the final list should look like the following list:
max_list = [['p_2', [3, 4, 5, 6], 8.4], ['p_3', [3, 7, 8, 9],9.1]]
I used the following code to modify the current list, but it does not work.
import itertools
for i, k in itertools.combinations(list, 2):
if i[1]==k[1]:
if i[3]>= k[3]:
list.remove(k)
else:
list.remove(i)
Thanks for your help
Your logic is right and you have almost found the solution. Here are your mistakes:
The inner list (e.g. ['p_1', [3, 4, 5, 6], 6.2]) contains three items. So, the index of the last item is 2 and not 3 as you have written in your code. You should write if i[2] >= k[2]: instead of if i[3] >= k[3]:.
You have to check that an element is in the list list before to remove it. if not, you will get the exception ValueError.
Based on your logic, here is the working code:
import itertools
list = [['p_1', [3, 4, 5, 6], 6.2],
['p_2', [3, 4, 5, 6], 8.4],
['p_3', [3, 7, 8, 9],9.1],
['p_4', [3, 4, 5, 6],8.2]]
max_list = list[:] # it is better to use a copy of the original list
for i, k in itertools.combinations(max_list, 2):
if i[1] == k[1]:
if i[2] >= k[2] and k in max_list:
max_list.remove(k)
elif i[2] < k[2] and i in max_list:
max_list.remove(i)
print(max_list) # [['p_2', [3, 4, 5, 6], 8.4], ['p_3', [3, 7, 8, 9], 9.1]]
Update: to count the max values like 'p_2' = 1 'p_3' = 1
counting = {}
for item in max_list:
counting[item[0]] = max_list.count(item)
print(counting) # {'p_2': 1, 'p_3': 1}
Update: new version of the counting.
list = list = [['point_21', [5, 18, 19, 21], 21.25],
['point_21', [5, 18, 19, 22], 22.05],
['point_21', [5, 18, 20, 21], 21.25],
['point_21', [5, 18, 20, 22], 22.01],
['point_21', [5, 18, 21, 22], 22.058],
['point_21', [5, 19, 20, 21], 21.5625],
['point_21', [5, 18, 19, 21], 21.25],
['point_20', [5, 17, 19, 22], 20],
['point_20', [5, 16, 20, 21], 21.252],
['point_20', [5, 19, 20, 22], 22.9],
['point_20', [5, 1, 21, 22], 22.6],
['point_20', [5, 12, 20, 21], 21.56]]
Assume, you have the list above. It contains 12 items. After running the alogithms that compute the max_list (i.e. the first code), you will have the list max_listthat contains 11 items, because one item have been removed (duplication).
max_list = [['point_21', [5, 18, 19, 22], 22.05],
['point_21', [5, 18, 20, 21], 21.25],
['point_21', [5, 18, 20, 22], 22.01],
['point_21', [5, 18, 21, 22], 22.058],
['point_21', [5, 19, 20, 21], 21.5625],
['point_21', [5, 18, 19, 21], 21.25],
['point_20', [5, 17, 19, 22], 20],
['point_20', [5, 16, 20, 21], 21.252],
['point_20', [5, 19, 20, 22], 22.9],
['point_20', [5, 1, 21, 22], 22.6],
['point_20', [5, 12, 20, 21], 21.56]]
For that, here is the counting algorithm:
counting = {}
points = set([item[0] for item in max_list if item[0]])
for point in points:
counting[point] = len([1 for item in max_list if item[0]==point])
print(counting) # {'point_21': 6, 'point_20': 5}
You can sort by the second element and then use itertools.groupby() to group the elements with the same second element. This will give you groups where all of the second element are the same. After that max() can be used based on the third element:
from itertools import groupby
from operator import itemgetter
second = itemgetter(1)
third = itemgetter(2)
max_list = [max(g, key=third) for k, g in groupby(sorted(l, key=second), key=second)]
# [['p_2', [3, 4, 5, 6], 8.4], ['p_3', [3, 7, 8, 9], 9.1]]

How to sum elements inside a nested list?

So, I have a list that looks something like this
[[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]]
And I want it to look like this
[[3, 7, 11],
[15, 19, 23],
[27, 31, 35],
[39, 43, 27]]
that is 3 = sum([1, 2]), 7 = sum([3, 4]), ....
I have tried nesting for loops but I haven't found anything that got the desired result, does anyone know how I could do this?
This will do the job quite nicely and imo is more readable than list comprehensions.
lists = [[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]]
new_lists = []
for nested in lists:
new_ls = []
for ls in nested:
new_ls.append(sum(ls))
new_lists.append(new_ls)
>>> new_lists
[[3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]]
You could also use list comprehensions:
[[sum(x) for x in triple] for triple in lists]
In the above list comprehension, triple will be your list of three doubles so the first for loop will be covering these. x will then be each list of doubles, inside of the triple so we are summing it, while keeping it inside the original triple by using this bracket around:
[sum(x) for x in triple]
output:
[[3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]]
If you are happy to use a 3rd party library, you can use NumPy and sum along a single dimension:
import numpy as np
A = np.array([[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]])
res = A.sum(2)
Result:
array([[ 3, 7, 11],
[15, 19, 23],
[27, 31, 35],
[39, 43, 47]])
See also: What are the advantages of NumPy over regular Python lists?

Categories

Resources