Pandas: compare list objects in Series
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
In my dataframe a column is made up of lists, for example:
df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])
I need to find out the location of list [1,2] in this dataframe. I tried:
df.loc[df['A'] == [1,2]]
and
df.loc[df['A'] == [[1,2]]]
but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?
python pandas
New contributor
add a comment |Â
up vote
6
down vote
favorite
In my dataframe a column is made up of lists, for example:
df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])
I need to find out the location of list [1,2] in this dataframe. I tried:
df.loc[df['A'] == [1,2]]
and
df.loc[df['A'] == [[1,2]]]
but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?
python pandas
New contributor
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
In my dataframe a column is made up of lists, for example:
df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])
I need to find out the location of list [1,2] in this dataframe. I tried:
df.loc[df['A'] == [1,2]]
and
df.loc[df['A'] == [[1,2]]]
but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?
python pandas
New contributor
In my dataframe a column is made up of lists, for example:
df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])
I need to find out the location of list [1,2] in this dataframe. I tried:
df.loc[df['A'] == [1,2]]
and
df.loc[df['A'] == [[1,2]]]
but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?
python pandas
python pandas
New contributor
New contributor
edited 1 hour ago
Seanny123
2,18833261
2,18833261
New contributor
asked 2 hours ago
Shiang Hoo
311
311
New contributor
New contributor
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
Do not using list
in cell , it create a lot of problem for pandas
.If you do need a object
columns , using tuple
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
add a comment |Â
up vote
5
down vote
With Numpy arrays
df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))
A B
0 [1, 2] True
1 [2, 4] False
2 [3, 1] False
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
add a comment |Â
up vote
3
down vote
You can use apply
and compare as:
df['A'].apply(lambda x: x==[1,2])
0 True
1 False
2 False
Name: A, dtype: bool
print(df[df['A'].apply(lambda x: x==[1,2])])
A
0 [1, 2]
add a comment |Â
up vote
2
down vote
Little messy solution using numpy
df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())
0 True
1 False
2 False
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Do not using list
in cell , it create a lot of problem for pandas
.If you do need a object
columns , using tuple
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
add a comment |Â
up vote
6
down vote
Do not using list
in cell , it create a lot of problem for pandas
.If you do need a object
columns , using tuple
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Do not using list
in cell , it create a lot of problem for pandas
.If you do need a object
columns , using tuple
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
Do not using list
in cell , it create a lot of problem for pandas
.If you do need a object
columns , using tuple
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
answered 2 hours ago
W-B
89.1k72653
89.1k72653
add a comment |Â
add a comment |Â
up vote
5
down vote
With Numpy arrays
df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))
A B
0 [1, 2] True
1 [2, 4] False
2 [3, 1] False
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
add a comment |Â
up vote
5
down vote
With Numpy arrays
df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))
A B
0 [1, 2] True
1 [2, 4] False
2 [3, 1] False
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
With Numpy arrays
df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))
A B
0 [1, 2] True
1 [2, 4] False
2 [3, 1] False
With Numpy arrays
df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))
A B
0 [1, 2] True
1 [2, 4] False
2 [3, 1] False
answered 1 hour ago
piRSquared
146k21128260
146k21128260
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
add a comment |Â
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
2
2
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
â jpp
1 hour ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
â ALollz
21 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
@ALollz yes and yes
â piRSquared
8 mins ago
add a comment |Â
up vote
3
down vote
You can use apply
and compare as:
df['A'].apply(lambda x: x==[1,2])
0 True
1 False
2 False
Name: A, dtype: bool
print(df[df['A'].apply(lambda x: x==[1,2])])
A
0 [1, 2]
add a comment |Â
up vote
3
down vote
You can use apply
and compare as:
df['A'].apply(lambda x: x==[1,2])
0 True
1 False
2 False
Name: A, dtype: bool
print(df[df['A'].apply(lambda x: x==[1,2])])
A
0 [1, 2]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can use apply
and compare as:
df['A'].apply(lambda x: x==[1,2])
0 True
1 False
2 False
Name: A, dtype: bool
print(df[df['A'].apply(lambda x: x==[1,2])])
A
0 [1, 2]
You can use apply
and compare as:
df['A'].apply(lambda x: x==[1,2])
0 True
1 False
2 False
Name: A, dtype: bool
print(df[df['A'].apply(lambda x: x==[1,2])])
A
0 [1, 2]
answered 2 hours ago
Sandeep Kadapa
3,524424
3,524424
add a comment |Â
add a comment |Â
up vote
2
down vote
Little messy solution using numpy
df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())
0 True
1 False
2 False
add a comment |Â
up vote
2
down vote
Little messy solution using numpy
df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())
0 True
1 False
2 False
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Little messy solution using numpy
df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())
0 True
1 False
2 False
Little messy solution using numpy
df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())
0 True
1 False
2 False
answered 1 hour ago
Vaishali
16k3927
16k3927
add a comment |Â
add a comment |Â
Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.
Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.
Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.
Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53102731%2fpandas-compare-list-objects-in-series%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password