'Getting AttributeError: 'list' object has no attribute 'split' when using list comprehension
I am trying to extract countries from the Locations column in a dataframe , each cell could have more than one Locations split by '|'. Currently I am doing this,
conditions = [
(len(org_df['Locations'].str.lower().str.split('|')) == 1),
(len(org_df['Locations'].str.lower().str.split('|')) > 1)]
values = [
list(org_df['Locations'].str.lower().str.split(','))[-1],
[x.split(',')[-1] for x in list(org_df['Locations'].str.lower().str.split('|'))]
]
org_df['Country'] = np.select(conditions,values)
This gives me AttributeError: 'list' object has no attribute 'split'
, if I do the same thing with a particular cell in the dataframe like this,
bruh = [x.split(',')[-1] for x in list(org_df.loc[97,'Locations'].split('|'))]
print(bruh)
I get the desired output,
[' India', ' India']
I am not able to understand how to resolve this, any help would be appreciated.
Update
I was trying to make a list of a list to store the values of one or more countries. And as @meh pointed out I did land up with more rows than my dataframe. For now I have done this
for x in org_df['Locations'].str.lower().str.split('|'):
loc_list = []
for y in x :
word = y.split(',')[-1].lower()
if(word.strip() in loc_list):
continue
else:
loc_list.append(word.strip())
loc_str= ','.join(loc_list)
country_list.append(loc_str)
Is there any way that I can use list comprehension to improve my code ?
Update Two Here is the output @Onur requested.
[{'Acronym': nan,
'Age': '35 Years to 64 Years \xa0 (Adult)',
'Completion Date': nan,
'Conditions': 'Breast Cancer|Cervical Cancer',
'Country': 'india',
'Enrollment': 151538.0,
'First Posted': 'March 10, 2008',
'Funded Bys': 'Other|NIH',
'Gender': 'Female',
'Interventions': 'Other: active surveillance|Other: educational intervention|Procedure: examination|Procedure: long-term screening',
'Last Update Posted': 'August 26, 2013',
'Locations': 'Tata Memorial Hospital, Mumbai, India',
'NCT Number': 'NCT00632047',
'Other IDs': 'CDR0000586791|TATA-1900215717A1',
'Outcome Measures': 'Effectiveness of well planned health education programs and low-cost screening methods (e.g., clinical breast exam and visual inspection of the cervix) in reducing the incidence of and mortality due to breast and cervical cancer',
'Phases': 'Phase 2',
'Primary Completion Date': 'December 2015',
'Rank': 1,
'Results First Posted': nan,
'Sponsor/Collaborators': 'Tata Memorial Hospital|National Cancer Institute (NCI)',
'Start Date': 'May 1998',
'Status': 'Unknown status',
'Study Designs': 'Allocation: Randomized|Primary Purpose: Screening',
'Study Documents': nan,
'Study Results': 'No Results Available',
'Study Type': 'Interventional',
'Title': 'Early Detection of Breast Cancer and Cervical Cancer in Women in India',
'URL': 'https://ClinicalTrials.gov/show/NCT00632047'}]
Solution 1:[1]
You have to recalcuate the offsets for the new resolution. Simply calculate a scale factor by which you can multiply the offsets and redraw the canvas. Pseudocode:
offset = (76.2, 75.2)
scale = 450/300 // 1.5
newOffset = offset * scale //(114.3, 112,5)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ti Hausmann |
