Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'Series' object has no attribute 'value'

I am trying to get a list of phone numbers

here is the code

response='108'
group="MAMA"
optout='False'

phone_numbers = merged_df.loc[(merged_df['Facility Code'] ==facility_number) & (merged_df['group'] == group) & (merged_df['Optedout'] == optout)]['phone'].values
    print(phone_numbers)

My dataframe looks like so

phone       group   County  PNC/ANC Facility Name   Optedout    Facility Code
25470000000 MAMA    Orange  PNC     Main Centre       FALSE      112
25470000000 MAMA    Orange  PNC     Main Centre       FALSE      112
25470000010 MAMA    Orange  PNC     Centre            FALSE      108
25470000020 MAMA    Orange  PNC     Centre            FALSE      108
25470000000 MAMA    Orange  PNC     Main Centre       FALSE      112

This is the error I get

AttributeError: 'Series' object has no attribute 'value'

desired output

[25470000010,25470000020]

I can't seem to figure out what I am doing wrong. kindly help me fix this

like image 410
Sista-Night Avatar asked Sep 01 '25 10:09

Sista-Night


2 Answers

You need to remove .values:

phone_numbers = merged_df.loc[(merged_df['Facility Code'] ==facility_number) & (merged_df['group'] == group) & (merged_df['Optedout'] == optout)]['phone']
like image 115
Mayank Porwal Avatar answered Sep 02 '25 23:09

Mayank Porwal


@Serge Ballesta's comment is the most likely cause.

There are typos in the code that you have shared. Check whether you called value instead of values.

The following code works as expected:

import pandas as pd

data = {'phone': [25470000000, 25470000000, 25470000010, 25470000020, 25470000000], 'group': ['MAMA', 'MAMA', 'MAMA', 'MAMA', 'MAMA'], 'County': ['Orange', 'Orange', 'Orange', 'Orange', 'Orange'], 'PNC/ANC': ['PNC', 'PNC', 'PNC', 'PNC', 'PNC'], 'Facility Name': ['Main Centre', 'Main Centre', 'Centre', 'Centre', 'Main Centre'], 'Optedout': ['FALSE', 'FALSE', 'FALSE', 'FALSE', 'FALSE'], 'Facility Code': [112, 112, 108, 108, 112]}

merged_df = pd.DataFrame.from_dict(data)

facility_number = 108
group = 'MAMA'
optout = 'FALSE'

phone_numbers = merged_df.loc[(merged_df['Facility Code'] ==facility_number) & (merged_df['group'] == group) & (merged_df['Optedout'] == optout)]['phone'].values

print(phone_numbers)

Output:

[25470000010 25470000020]

By removing .values, the output is a dataframe:

2    25470000010
3    25470000020
Name: phone, dtype: int64
like image 30
Hari Avatar answered Sep 03 '25 01:09

Hari