У меня есть словарь в словаре.И моя проблема заключается в том, что я хочу выполнить операцию or
над индексом ключей ('filter'
и 'filteer'
) 0 в словаре.На основании этого результата я хочу применить операцию groupby
к определенному столбцу в индексе 1 в словаре.(например, если бренд (условия [0]) == AMBI (условия [8]) (или) производитель (условия [1]) == AMBI (условия [8]) я хочу вернуть фрейм данных, и на этом фрейме данных я хочувыполнить групповые операции над одним из столбцов.)
Мой код:
import csv
import pandas as pd
import sys
class sample:
def create_df(self, f):
self.z=pd.read_csv(f)
def get_resultant_df(self, list_cols):
self.data_frame = self.z[list_cols[:]]
def process_df(self, df, conditions):
resultant_df = self.data_frame
if conditions[2] == 'equals':
new_df =resultant_df[resultant_df[conditions[1]] == conditions[3]]
return new_df
elif conditions[2] == 'contains':
new_df = resultant_df[resultant_df[conditions[1]].str.contains(conditions[3])]
return new_df
elif conditions[2] == 'not equals':
new_df = resultant_df[resultant_df[conditions[1]] != conditions[3]]
return new_df
elif conditions[2] == 'startswith':
new_df = resultant_df[resultant_df[conditions[1]].str.startswith(conditions[3])]
return new_df
elif conditions[2] == 'in':
new_df = resultant_df[resultant_df[conditions[1]].isin(resultant_df[conditions[3]])]
return new_df
elif conditions[2] == 'not in':
new_df = resultant_df[~resultant_df[conditions[1]].isin(resultant_df[conditions[3]])]
return new_df
elif conditions[2] == 'group':
new_df = list(resultant_df.groupby(conditions[0])[conditions[1]])
return new_df
elif conditions[2] == 'specific':
new_df = resultant_df.loc[resultant_df[conditions[0]] == conditions[8]]
return new_df
elif conditions[2] == 'same':
new_df = resultant_df[(resultant_df[conditions[0]] == conditions[8]) & (resultant_df[conditions[1]] == conditions[8])]
return new_df
elif conditions[2]=='trail':
new_df={0:{'filter'{'filter1':resultant_df.loc[resultant_df[conditions[0]] == conditions[8]]},'filteer':{'filter1':resultant_df.loc[resultant_df[conditions[0]] == conditions[8]]}},
1:{'group':{resultant_df.groupby(new_df[0][filter])}}}
return new_df
if __name__ =='__main__':
sample = sample()
sample.create_df("/home/purpletalk/GrammarandProductReviews.csv")
df = sample.get_resultant_df(['brand', 'reviews.id','manufacturer','reviews.title','reviews.username','id','dateAdded','reviews.rating'])
new_df = sample.process_df(df, ['brand','manufacturer','trail','Windex', 'size', 'equal',8,700,'AMBI'])
print (new_df[1][group])
Может кто-нибудь помочь мне?Приведенный выше код возвращает ошибки, и я хочу знать, как можно выполнить операцию or
.