Как использовать a для l oop для добавления элементов в список? - PullRequest
0 голосов
/ 01 августа 2020
price
    price      date        fruit
0   0.83    2010-01-04      banana
1   0.05    2010-01-04      appple

Как вы можете использовать формулу oop для исправления элементов в списке? Моя цель:

price = price.set_index('date').dropna(how='all').resample('W').last()```
keep if fruit == banana
drop the column fruit
perform calculations
repeat for all other fruits in the dataset price

Я пробовал

fruits = ['apple', 'banana', 'pair']
listxx = [(price, "price")]
for fruit in fruits:
    for i, (x, y) in enumerate(listxx):
        listxx[i] = x['fruit'] == fruit
        listxx[i] = x.set_index('date').dropna(how='all').resample('W').last()
        listxx[i].drop(listxx[i].columns[fruit], axis=1, inplace=True)
        perform calculations

1 Ответ

1 голос
/ 01 августа 2020

Думаю, вы можете:

# convert to datetime and set index
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

# do calculations
df = (df
     .groupby('fruit', as_index=False)
     .resample('W')
     .last()
     .drop("fruit", axis=1))

print(df)
             
  date         price    
0 2010-01-10   0.05
1 2010-01-10   0.83
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...