selecting rows in a tuplelist in pandas - python

I have a list of tuples as below in python:
Index Value
0 (1,2,3)
1 (2,5,4)
2 (3,3,3)
How can I select rows from this in which the second value is less than or equal 2?
EDIT:
Basically, the data is in the form of [(1,2,3), (2,5,4), (3,3,3)....]

You could slice the tuple by using apply:
df[df['Value'].apply(lambda x: x[1] <= 2)]
Seems, it was a list of tuples and not a DF:
To return a list back:
data = [item for item in [(1,2,3), (2,5,4), (3,3,3)] if item[1] <= 2]
# [(1, 2, 3)]
To return a series instead:
pd.Series(data)
#0 (1, 2, 3)
#dtype: object

df[df["Value"].str[1] <= 2]
You can read this for more details - http://pandas.pydata.org/pandas-docs/stable/text.html#indexing-with-str

just to put this out there. you should read mcve. your question was/is confusing because it looks as though you've got 2 good answers and your not satisfied. then you edited your question to be clearer, but just barely.
ok, now that i've got my editorial out of the way.
setup
this is what i'm assuming i'm working with
# list of tuples
lot = [(1, 2, 3), (2, 5, 4), (3, 3, 3)]
desired output
i think. btw, nothing to do with pandas at all
[(a, b, c) for a, b, c in lot if b <= 2]
[(1, 2, 3)]
with pandas
however, since you did tag this pandas
s = pd.Series(lot)
#TrigonaMinima's answer, give them credit if this works for you.
s[s.str[1].le(2)]
0 (1, 2, 3)
dtype: object

Related

Is this a reasonable way for choosing a certain index from a list by considering the elements of a corresponding list in python?

I am relatively new to programming, and have this problem:
There are two lists: C=[i,i,k,l,i] and D =[m,n,o,p,q]
I want to select the index of the minimum element of C. If k or l is the minimum, it is quite simple, since the min function will directly return the desired index. But if i is the minimum, there are several possibilities. In that case, I want to look at list D's elements, but only at the indices where i occurs in C. I then want to chose my sought-after index based on the minimum of those particular elements in D
I thought of the following code:
min_C = min(C)
if C.count(min_C) == 1:
soughtafter_index = C.index(min_C)
else:
possible_D_value = []
for iterate in C:
if iterate==min_C:
possible_index = C.index(iterate)
possible_D_value.append(D[possible_index])
best_D_value = min(possible_D_value)
soughtafter_index = D.index(best_D_value)
(Note that in the problem C and D will always have the same length)
I havent had a chance to test the code yet, but wanted to ask whether it is reasonable? Is there a better way to handle this? (and what if there is a third list-- then this code will get even longer...)
Thank you all
Try this:
soughtafter_index = list(zip(C, D)).index(min(zip(C,D)))
UPDATE with the required explanation:
>>> C = [1, 5, 1, 3, 1, 4]
>>> D = [0, 1, 1, 3, 0, 1]
>>> list(zip(C, D))
[(1, 0), (5, 1), (1, 1), (3, 3), (1, 0), (4, 1)]
>>> min(zip(C, D))
(1, 0)

How to remove several elements of a list by the index which can be changed each time?

I have a list like below:
A = [1, 2, 3, 4]
After using enumerate I have the following list:
A = [(0, 1), (1, 2), (2, 3), (3, 4)]
After checking a condition, I realized that I don't need the elements with index 0 and 2.
It means that my condition returns a list like below which can be different each time:
condA = [(0, 1), (2, 3)]
I know that I can use del or .pop() to remove an element from a list.
However, I was wondering how can I read the numbers like (0) and (2) in my condA list and remove those elements from my original list.
I don't want to enter the 0 and 2 in my code because each time they would be different.
The result would be like this:
A_reduced = [2, 4]
If you want to read the index from the condA list and create the list of that number, the list of indices of elements to be removed will be like:
rm_lst = [x[0] for x in condA]
Now, to remove the elements from your main list:
A = [(0, ((11), (12))), (1, ((452), (54))), (2, ((545), (757))), (3, ((42), (37)))]
A_reduced = [x[1] for x in A if x[0] not in rm_lst]
Final Code:
A = [(0, ((11), (12))), (1, ((452), (54))), (2, ((545), (757))), (3, ((42), (37)))]
condA = [(0, ((11), (452))), (2, ((545), (757)))]
rm_lst = [x[0] for x in condA]
A_reduced = [x[1] for x in A if x[0] not in rm_lst]
print(A_reduced)
If you want to delete elements from list inside loop, you should iterate from last to first:
for i in range(len(A) - 1, -1, -1):
if true: # replace with condition
del A[i]
Upd.
You can also use list comprehension for this, but you should invert you condition (A[i][0] != 11 => A[i][0] == 11):
A = [A[i] for i in range(len(A)) if inverted_condition]
Loop through condA, pop the element off the list A. You need a counter to decrease the indexes, since the size of A is shrinking. Make sure to sort condA:
A = [1, 2, 3, 4]
condA = [(0, 1), (2, 3)]
i = 0
for item in condA:
A.pop(item[0]-i)
i+=1
#result: [2, 4]
IIUC maybe a function is the correct way:
A = [1, 2, 3, 4]
def remove_items(lis, idx):
lis2=lis.copy()
[lis2.pop(i) for i in idx]
return lis2
A_reduced=remove_items(A,[0,2])
Output:
Out[32]: [2, 3]
You can add any list of indexes you want, and it'll drop them from the list. (If they exist)
Edit: Adjusted to your new values, and modified the function so you also keep your original list (if that's needed)

Access individual elements of tuples of dictionary keys

Considering the code snippet below -
list1 = [1,2,3,4]
list2 = [1,2,3,4]
list3 = ['a','b','c','d']
dct = dict(zip(zip(list1,list2),list3))
print(dct)
gives me,
{(1, 1): 'a', (2, 2): 'b', (3, 3): 'c', (4, 4): 'd'}
Now,
print(dct.keys())
gives me,
dict_keys([(1, 1), (2, 2), (3, 3), (4, 4)])
How can i access first element of the above list of keys?
Something like -
dct.keys[0, 0] = 1
dct.keys[0, 1] = 1
dct.keys[1, 0] = 2
dct.keys[1, 2] = 2
and so on...
Remember that a dict is unordered, and that dict.keys() may change order.
That said, to access the first element of a list, as you said, you can use list[element_index]. If the elemnt is an iterable, do that again!
So it would be
dct_keys = list(yourdict.keys())
dct_keys[0][0] = 1
dct_keys[0][1] = 1
dct_keys[1][0] = 2
dct_keys[1][1] = 2
You need to first convert the dct.keys() output to a list, and then the problem reduces to simple list-of-tuples indexing. To convert your .keys() output to a list, there are multiple available ways (check this out). Personally, I find using list comprehension as one of the simplest and most generic ways:
>>> [key for key in dct.keys()]
[(1, 1), (2, 2), (3, 3), (4, 4)]
And now simply index this list of tuples as:
>>> [key for key in dct.keys()][0][0]
1
Hope that helps.

Python- For Loop - Increment each index position for each tuple in list

I've searched around for a possible way to do this. I'm trying to make a loop that will go through my list of tuple pairs. Each index contains data that I will calculate and append to a list through each loop run until the end of the list of tuples is reached. Currently using a for loop, but I might use while loop.
index_tuple = [(1, 2), (2, 3), (3, 4)]
total_list = []
for index_pairs in index_tuple:
total_list.append(index_tuple[0][1] - index_tuple[0][0])
What I'm trying to get the loop to do:
(index_tuple[0][1] - index_tuple[0][0])#increment
(index_tuple[1][1] - index_tuple[1][0])#increment
(index_tuple[2][1] - index_tuple[2][0])#increment
Then I guess my final question is it possible to increment index position with a while loop?
Use a list comprehension. This iterates the list, unpacks each tuple to two values a and b, then it subtracts the first item from the second and inserts this new subtracted value into the new list.
totals = [b - a for a, b in index_tuple]
A list comprehension is the best solution for this problem, and Malik Brahimi's answer is the way to go.
Nevertheless, sticking with your for loop, you need to reference index_pairs in the body of the loop because this variable is assigned each tuple from index_tuple as the loop iterates. You do not need to maintain an index variable. A corrected version would be this:
index_tuple = [(1, 2), (2, 3), (3, 4)]
total_list = []
for index_pairs in index_tuple:
total_list.append(index_pairs[1] - index_pairs[0])
>>> print total_list
[1, 1, 1]
A cleaner version which unpacks the tuples from the list directly into 2 variables would be:
index_tuples = [(1, 2), (2, 3), (3, 4)]
total_list = []
for a, b in index_tuples:
total_list.append(b - a)
>>> print total_list
[1, 1, 1]
You also asked about using a while loop to achieve the same. Use an integer to keep track of the current index and increment it by one on each iteration of the loop:
index_tuples = [(1, 2), (2, 3), (3, 4)]
total_list = []
index = 0
while index < len(index_tuples):
total_list.append(index_tuples[index][1] - index_tuples[index][0])
index += 1
>>> print total_list
[1, 1, 1]

making list in python

When i executed the following python script
list= (1,2,3,4,1,2,7,8)
for number in list:
item1= number
item2= list[list.index(item1)+2]
couple= item1, item2
print couple
the goal is to link each number with the second following
I obtain this result
(1, 3)
(2, 4)
(3, 1)
(4, 2)
(1, 3)
(2, 4)
(and then the index gets out of range but this is not the problem)
My question is why the number 1 in the fifth line is still coupled to the number 3 and how can i make that it is coupled to the number 7; idem for the number 2 in the sixth line that should be coupled to the number 8.
additional question
what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]
list.index returns the offset of the first occurrence of the value in the list - thus if you do [1,1,1].index(1), the answer will always be 0, even though 1 and 2 are also valid answers.
Instead, try:
from itertools import islice, izip, ifilter
mylist = [1,2,3,4,1,2,7,8]
for pair in ifilter(lambda x: x[0]==1, izip(mylist, islice(mylist, 2, None))):
print pair
results in
(1, 3)
(1, 7)
xs.index(x) gives you the index of the first occurence of x in xs. So when you get to the second 1, .index gives you the index of the first 1.
If you need the index alongside the value, use enumerate: for i, number in enumerate(numbers): print number, numbers[i+2].
Note that I deliberately didn't use the name list. It's the name of a built-in, you shouldn't overwrite it. Also note that (..., ...) is a tuple (and therefore can't be changed), not a list (which is defined in square brackets [..., ...] and can be changed).
You have duplicates in the list so index always returns the first index.
Start your program with for index in range(len(list) - 1)
You are using .index which returns the first occurrence of number.
consider:
for number in range(len(list)):
item1= list[number]
item2= list[number+2]
couple= item1, item2
print couple
>>> zip(lst, lst[2:])
[(1, 3), (2, 4), (3, 1), (4, 2), (1, 7), (2, 8)]
To get only pairs (1, X):
>>> [(a, b) for (a, b) in zip(lst, lst[2:]) if a == 1]
[(1, 3), (1, 7)]
Recommended reading:
http://docs.python.org/tutorial/datastructures.html
http://docs.python.org/howto/functional.html

Categories

Resources