Last active
October 28, 2020 23:42
-
-
Save benfasoli/bc904dbe45c57b4e939ee38d6a51b3f2 to your computer and use it in GitHub Desktop.
Example of `io.StringIO` to pass text to `pd.read_csv(text)`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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