Search code examples
pythonenumerate

Python in enumerate not giving expected output


I'm having an issue with output from in enumerate function. It is adding parenthesis and commas into the data. I'm trying to use the list for a comparison loop. Can anyone tell me why the special characters are added resembling tuples? I'm going crazy here trying to finish this but this bug is causing issues.

# Pandas is a software library written for the Python programming language for data manipulation and analysis.
import pandas as pd
#NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays
import numpy as np
df=pd.read_csv("https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/dataset_part_1.csv")
df.head(10)
df.isnull().sum()/df.count()*100
df.dtypes
# Apply value_counts() on column LaunchSite
df[['LaunchSite']].value_counts()
# Apply value_counts on Orbit column
df[['Orbit']].value_counts()
 #landing_outcomes = values on Outcome column
landing_outcomes = df[['Outcome']].value_counts()
print(landing_outcomes)

#following causes data issue
for i,outcome in enumerate(landing_outcomes.keys()):
    print(i,outcome)

#following also causes an issue to the data
bad_outcomes=set(landing_outcomes.keys()[[1,3,5,6,7]])
bad_outcomes

# landing_class = 0 if bad_outcome
# landing_class = 1 otherwise
landing_class = []
for value in df['Outcome'].items():
    if value in bad_outcomes:
        landing_class.append(0)
    else:
        landing_class.append(1)
df['Class']=landing_class
df[['Class']].head(8)
df.head(5)
df["Class"].mean()

The issue I'm having is

for i,outcome in enumerate(landing_outcomes.keys()):
    print(i,outcome)

is changing my data and giving an output of

0 ('True ASDS',)
1 ('None None',)
2 ('True RTLS',)
3 ('False ASDS',)
4 ('True Ocean',)
5 ('False Ocean',)
6 ('None ASDS',)
7 ('False RTLS',)

additionally, when I run

bad_outcomes=set(landing_outcomes.keys()[[1,3,5,6,7]])
bad_outcomes

my output is

{('False ASDS',),
 ('False Ocean',),
 ('False RTLS',),
 ('None ASDS',),
 ('None None',)}

I do not understand why my data return is far from expected and how to correct it.


Solution

  • Try this

    for i, (outcome,) in enumerate(landing_outcomes.keys()):
        print(i, outcome)
    

    Or

    for i, outcome in enumerate(landing_outcomes.keys()):
        print(i, outcome[0])