Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # import modules
  2. import pandas as pd
  3.  
  4. # import dataframe from .csv
  5. data = pd.read_csv("./data.csv")
  6.  
  7. # create empty dataframe for output
  8. columnNames = ['user', 'trial', 'reaction_time']
  9. newData = pd.DataFrame(columns=columnNames)
  10.  
  11. processedTrials = [] # used to mark trials that have been processed
  12. currentUser = 0 # the current user being processed
  13.  
  14. for index, row in data.iterrows():
  15.     print("Processing row ", index, "...")
  16.  
  17.     # if processing a new user, clear the list of processed trials and assign new user as current
  18.     if row['user'] != currentUser:
  19.         currentUser = row['user']
  20.         processedTrials = []
  21.    
  22.     # if selected trial has already been processed, skip
  23.     if row['trial'] in processedTrials:
  24.         print("Skipping row")
  25.         continue
  26.     # otherwise, save data to new dataframe
  27.     else:
  28.         print("Appending data...")
  29.         processedTrials.append(row['trial'])
  30.         newData = newData.append({'user':row['user'], 'trial':row['trial'], 'reaction_time':row['\'reaction_time_2\'']}, ignore_index=True)
  31.  
  32. # print sample of output and save to .csv
  33. print(newData.head())
  34. newData.to_csv('./newData.csv', index=False, encoding='utf-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement