month_kw_rank.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pandas as pd
  2. import time
  3. def day_col(row):
  4. result = row['dt'][0:10]
  5. return result
  6. custom_name = ['鑫富']
  7. for name in custom_name:
  8. df = pd.read_csv(f"C:\\Users\\s1301\\Documents\\1月{name}排名前10.csv",encoding='utf-8')
  9. df['dt2'] = df.apply(day_col, axis=1)
  10. df = df.drop_duplicates(subset=['dt2','kw'])
  11. df_kw_rank = df[['dt2','kw','ranking']].sort_values(by='dt2')
  12. df_kw_rank_2 = df_kw_rank.reset_index(drop=True)
  13. df_kw_rank_2.columns = ['日期','關鍵字','名次']
  14. print(df_kw_rank_2)
  15. # df_=pd.DataFrame(columns=list(' '))
  16. # print(pd.concat([df_kw_rank,df_]))
  17. data = []
  18. num = df.groupby('dt2',as_index=False).size()
  19. for index,row in num.iterrows():
  20. data.append([row['dt2'],row['size'],20,row['size']*20])
  21. df_first = pd.DataFrame(data,columns=['日期','關鍵字出現次數','首頁日費','首頁小計'])
  22. #前三名
  23. df_top_3 = df.loc[df['ranking']<=3]
  24. num_top_3 = df_top_3.groupby('dt2',as_index=False).size()
  25. data_2 = []
  26. for index,row in num_top_3.iterrows():
  27. data_2.append([row['dt2'],row['size'],40,row['size']*40])
  28. df_second = pd.DataFrame(data_2,columns=['日期','前3名字組數量','前3名字組日費','前3名字組小計'])
  29. df_result = pd.merge(df_first,df_second,on='日期',how='outer').fillna(101)
  30. new = pd.concat([df_kw_rank_2,df_result],axis=1)
  31. # df_result.insert(0,'日期 ',df_kw_rank['日期'])
  32. # df_result.insert(1,'關鍵字 ',df_kw_rank['關鍵字'])
  33. # df_result.insert(2,'名次 ',df_kw_rank['名次'])
  34. new.to_csv(f"C:\/Users\/s1301\/Documents\/{name}1月績效報表.csv",index=False)