lets say I have the following pandas dataframe. I would like to filter the row, where for example the name is Julia. But how can I filter if the names are in a list?
| Index | Department | Names |
|-------|------------| --------------------|
| 0 | History |[Carl, Julia, Jens] |
| 1 | Economics |[Maggie, Lisa, Jules]|
| 2 | Physics | [Freddy, Mark, Rolf]|
I have tried with df.contains()
but I receive a error message.
Any ideas?
CodePudding user response:
You can try:
df.loc["Julia" not in df.Names,:];
Hope it solves the problem.
CodePudding user response:
you could try converting the list into a string with join and then use contains():
df = pd.DataFrame([
['History',['Carl', 'Julia', 'Jens']],
['Economics',['Maggie', 'Lisa', 'Jules']],
['Pysics',['Freddy', 'Mark', 'Rolf']],
])
df[df[1].str.join(',').str.contains('Julia')]
0 1
0 History [Carl, Julia, Jens]
CodePudding user response:
You need to iterate the DataFrame to find the list item on each line:
for index, line in df.iterrows():
if 'Julia' in line['Names']:
print(line)
Department History
Names [Carl, Julia, Jens]
Name: 0, dtype: object