Home > database >  How Do For loop to make short my statement
How Do For loop to make short my statement

Time:12-01

I think it would be a simple question but I really stuck on it! how can I use for loop to make my statement more complex and short? I need the output be the exactlyenter image description here same format this is a code

courses_data = pd.read_csv('.........')

selected_features = ['course_name','course_link','university_name','course_type',
                     'university_logo', 'time_required', 'course_language', 
                     'course_subtitles', 'course_skills', 'course_rating', 
                     'category', 'sub_category', 'course_level']

combined_features = courses_data['course_name'] ' ' courses_data['course_link'] ' ' courses_data['university_name'] ' ' courses_data['course_type'] ' ' courses_data['university_logo'] ' ' courses_data['time_required'] ' ' courses_data['course_language'] ' ' courses_data['course_subtitles'] ' ' courses_data['course_skills'] ' ' courses_data['course_rating'] ' ' courses_data['category'] ' ' courses_data['sub_category'] ' ' courses_data['course_level']
print(combined_features)

CodePudding user response:

Something along these lines should work:

combined_features = courses_data[selected_features[0]]
for feature in selected_features[1:]:
  combined_features  = ' '   courses_data[feature]

CodePudding user response:

Since you are dealing with a pandas dataframe you could just do

df2 = courses_data[selected_features].copy()
print(df2)

OR

df2 = courses_data.filter(selected_features, axis=1)
print(df2)

Source

CodePudding user response:

combined_features = ""
selected_features = ['course_name','course_link','university_name','course_type',
                     'university_logo', 'time_required', 'course_language',
                     'course_subtitles', 'course_skills', 'course_rating',
                     'category', 'sub_category', 'course_level']

for feature in selected_features:
    combined_features  = courses_data[feature]   " "

print(combined_features)
  • Related