Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. datafilepath="/tmp/data.csv"
  5. colnames="tstamp, spend, budget, impression"
  6.  
  7. #A date parsing function to feed to read_csv
  8. #Expects string in the format YYYY-MM-dd HH:MM:SS TZ
  9. #Though it doesnt return a timezone aware datetime
  10. def dateparser(datestr):
  11. dt = datetime.datetime.strptime(datestr,'%Y-%m-%d %H:%M:%S %Z')
  12. return dt
  13.  
  14. #Dataframe read_csv
  15. df = pd.read_csv(datafilepath, names=colnames, header=0,date_parser=dateparser, parse_dates=['tstamp'])
  16. df.head()
  17.  
  18. #Add a column with no values
  19. df['new_column'] = np.nan
  20.  
  21. #Append all data from one data frame to other
  22. df.append(anotherDataFrame)
  23.  
  24. #Check and return all rows with boolean for blank columns
  25. #checks if the column 'spend' is null
  26. df[pd.isnull(df['spend']) == True]
  27.  
  28. #Iterate on rows of dataframe
  29. for index, row in df.iterrows():
  30. print "[rownum=%s] Time: %s " % (index, row('tstamp'))
  31.  
  32.  
  33. #Drop a column
  34. df = df.drop(['spend', axis=1)
Add Comment
Please, Sign In to add comment