Skip to content

Instantly share code, notes, and snippets.

@benfasoli
Last active October 28, 2020 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfasoli/bc904dbe45c57b4e939ee38d6a51b3f2 to your computer and use it in GitHub Desktop.
Save benfasoli/bc904dbe45c57b4e939ee38d6a51b3f2 to your computer and use it in GitHub Desktop.
Example of `io.StringIO` to pass text to `pd.read_csv(text)`
#!/usr/bin/env python3
import io
import pandas as pd
api_response_csv = """
x, y, z
1, 2, 3
4, 5, 6
"""
buffer = io.StringIO(api_response_csv)
api_response_df = pd.read_csv(buffer)
print(api_response_df)
# x y z
# 0 1 2 3
# 1 4 5 6
print(api_response_df.info())
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 2 entries, 0 to 1
# Data columns (total 3 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 x 2 non-null int64
# 1 y 2 non-null int64
# 2 z 2 non-null int64
# dtypes: int64(3)
# memory usage: 176.0 bytes
# None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment