Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Turning loop of string inputs from Excel into Python lists

I have an Excel file with three columns (Prefix, Feature, Values). All three are strings in Excel, but I need to turn the ‘Values’ entry from a string into a list when Prefix and Column equal the loop values.

Data looks like this:

Prefix Feature Value
Prefix_1 Feature1 Value1,Value2,Value3
Prefix_1 Feature2 Value4,Value5
Prefix_1 Feature3 Value6,Value7,Value8,Value9
Prefix_2 Feature4 Value10

It loops through all prefixes, then through all features, and I want to return the values in a list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

parametric_values = str(filtered_input.loc[filtered_input.Feature==column,'Value']).split(',')

filtered_input is a dataframe that is filtered to only the relevant prefixes and column is the value from the loop.

I would have expected parametric_values to be ['Value1','Value2','Value3'] for the first loop, but it returned as ['0 Value1', 'Value2', 'Value3\nName: Value', ' dtype: object']

I’m not sure why it returned a 0 to start with or the name and dtype object. What would I need to change with my code to get it to just return the values in the list?

>Solution :

Use this instead:

filtered_input.loc[filtered_input.Feature==column,'Value'].str.split(',').squeeze()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading