furas

pandas and io.StringIO - read data from string

Jul 20th, 2018
226
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×