SHARE
TWEET

pandas and io.StringIO - read data from string

furas Jul 20th, 2018 (edited) 144 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. text = '''             query        tags
  2. 0               hi       intro
  3. 1      how are you       wellb
  4. 2            hello       intro
  5. 3        what's up       wellb
  6. 4       how's life       wellb
  7. 5              bye          gb
  8. 6    see you later          gb
  9. 7         good bye          gb
  10. 8           thanks   gratitude
  11. 9        thank you   gratitude
  12. 10  that's helpful   gratitude
  13. 11      I am great  revertfine
  14. 12            fine  revertfine
  15. 13       I am fine  revertfine
  16. 14            good  revertfine'''
  17.  
  18. import pandas as pd
  19. from io import StringIO
  20.  
  21. fd = StringIO(text) # create file-like object in memory
  22.  
  23. df = pd.read_table(fd, sep="\s{2,}") # read from file-like object, use regex to seperate columns
  24. #df = pd.read_csv(fd, sep="\s{2,}")
  25.  
  26. print(df.columns)
  27. print(df)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top