Let's say I have the following data framework:
elements = [1,1,1,1,1,2,3,4,5]
df = pd.DataFrame({'elements': elements})
df.set_index(['elements'])
print df
elements
0 1
1 1
2 1
3 1
4 1
5 2
6 3
I have a list [1, 1, 2, 3]and I want a subset of the data frame, including these 4 elements, for example:
elements
0 1
1 1
5 2
6 3
I managed to handle this by building a dict, counting the occurrences of the elements in the array and creating a new dataframe, adding the substrings of the original.
Do you know some dataframe methods to help me find a more elegant solution?
After the comment by @jezrael: I have to add that I need to track the starting index (in df format).
We can see df (the first dataframe) as a resource repository, and I need to keep track of which rows / indexes are assigned:
: df 1, 2 3. , 0 1 1, 4 2 5 3.