I already know how to append a value depending on a for with an if loop but I want to know if there is an optimized way to do it.
Here is the solution:
column=[]
for i in range(movies.shape[1]):
if ((movies.dtypes[i]==float) | (movies.dtypes[i]==int)):
column.append(movies.columns[i])
print(column)
['title_year', 'aspect_ratio', 'duration', 'duration.1', 'budget', 'imdb_score', 'gross']
Where movies is a dataset.
I've tried with this:
column=[movies.columns[i] if ((movies.dtypes[i]==float) | (movies.dtypes[i]==int)) else 0 for i in range(movies.shape[1])]
But the result is:
[0, 'title_year', 0, 'aspect_ratio', 'duration', 0, 0, 'duration.1', 0, 0, 0, 0, 0, 0, 0, 0, 'budget', 'imdb_score', 'gross']
I had to put that 0 in the else sentence because without it I get a syntax error.
So, can I put those 3 lines in just one sentence?