0

This is my first question here, please pardon me if I make any mistakes.

I have two lists, List A and List B. List A has 'Date' associated with it, whereas, List B is a result of some calculations and it doesn't have any Date with it. The only thing common between these two is the column name (I have underlined it in the figures, they are not in ascending order though in List B), which is similar in both lists.

List A was the result of the following code:

number_of_columns = len(df.columns)
List_A= []

# loop over column indexes with step size 1
for i in range(0, number_of_columns, 1):
    List_A.append(df.iloc[:, i][~np.isnan(df.iloc[:, i])])

And List B is the result of the following code:

def cusum(df, page_name):  
  pages = pd.unique(df['page_name']) #get all page names from df
  master_df = pd.DataFrame()
  dfs = []

  for i in range(0,len(pages)):
    S = [] #create empty cusum list
    S.insert(0,0) #first entry to be zero
    name_of_page = pages[i]
    temp_df = df[(df.page_name == name_of_page)]
    mean  = temp_df['Engagement'].mean()
    temp_df.reset_index(inplace=True)
    Date = temp_df.Timestamp.values.astype(float)

    for i in temp_df.index:
      a = (S[i] + (temp_df['Engagement'][i] - mean))
      S.append(round(a,3))
      # S.extend(Date)
    
    # master_df = pd.DataFrame({name_of_page:S})
    del S[0]
    dfs.append(pd.DataFrame({name_of_page:S}))
    S.clear()

  master_df = pd.concat(dfs, axis=1)
  return dfs

List_B = []
List_B = cusum(original_df,'page_name')

And this is how my :original_df looks like

I would like to keep the Date from List A and values from List B based on the "names". Any guidance how can I do that?

AneesBaqir
  • 423
  • 1
  • 12
  • Show textual data as properly formatted text in the question, not as image or external link. If you are using a library like pandas, add the appropriate tag to the question. Show your own effort and code (properly formatted in the question). – Michael Butscher Sep 29 '21 at 10:47
  • Your images seem to suggest that you have two dataframes - not lists. Please clarify –  Sep 29 '21 at 10:53
  • Thank you for your response. I understand your point, however the problem is, these lists are the results of some computations and the resulted in this way. Typically, I know the list is something like this 'a = [1, 2, 3]', but here the DataFrame columns were transformed into list. I am not sure if I am able to explain the problem on hand correctly. – AneesBaqir Sep 29 '21 at 10:53
  • @BrutusForcus, it is list. I used the following code to convert my DataFrame into list. `number_of_columns = len(test_pro.columns) split_dfs = [] # loop over column indexes with step size 1 for i in range(0, number_of_columns, 1): split_dfs.append(test_pro.iloc[:, i][~np.isnan(test_pro.iloc[:, i])])` – AneesBaqir Sep 29 '21 at 10:55
  • Show relevant code properly formatted in the question, not in a comment. – Michael Butscher Sep 29 '21 at 10:58
  • @MichaelButscher, I updated the question with the edits. :) – AneesBaqir Sep 29 '21 at 11:10
  • Welcome to StackOverflow. I think you are attempting to do an SQL operation in Pandas called an `inner join`. I suggest that you look at https://stackoverflow.com/questions/53645882/pandas-merging-101 . You want to join on `name`. – rajah9 Sep 29 '21 at 11:33

0 Answers0