Assume I have a dataframe in Pandas:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
'B': 'one one two three two two one three'.split(),
'C': '0 1 2 3 4 5 6 7'.split(),
'D': '0 2 4 6 8 10 12 14'.split()})
The dataframe df looks like
A B C D
0 foo one 0 0
1 bar one 1 2
2 foo two 2 4
3 bar three 3 6
4 foo two 4 8
5 bar two 5 10
6 foo one 6 12
7 foo three 7 14
Note that the type of numbers in C and D columns is string.
I'm thinking about two conditions:
(1) Consecutive search
I want to return the D values when I'm searching C=2:5. If I use df.loc[df['C'] == '2:5', "D"], it returns an error. How can I do this part?
(2) Discrete search
I'd like to return the D values when I'm searching C=0,3,6. Again, if I use df.loc[df['C'] == '0,3,6', "D"], it returns an error. What should I write this code?