Maria

mail@pastecode.io avatar
unknown
python
2 years ago
1.0 kB
2
Indexable
Never
import pandas as pd


def task2():
    df = pd.read_csv('songs.csv')

    # Выбрать строк 20% из середины
    df_len = len(df.values)
    first_ind = int(df_len*0.4)
    last_ind = int(df_len - first_ind)
    indices = []
    for i in range(first_ind, last_ind):
        indices.append(i)
    values = df.take(indices)

    # Создать столбец target Target=(rank-peakPos)/weeks+e
    target = []
    for index, row in values.iterrows():
        target.append((row['rank']- row['peakPos'])/row['weeks'])
    values['target'] = target

    # Изменить значения в столбце Change = rank - lastPos
    for index, row in values.iterrows():
        row['change'] = row['rank'] - row['lastPos']

    # Создать столбец Class
    _class = []
    for index, row in values.iterrows():
        if ' love ' in row['title']:
            _class.append(1)
        else:
            _class.append(0)
    values['class'] = _class


if __name__ == '__main__':
    task2()