Advertisement
furas

pandas and io.StringIO - read data from string

Jul 20th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement