Issue
How to save Pandas df.info()
function output to variable or data frame?
I tried using buffer value but it is not saving output neatly.
Code:
import io
buffer = io.StringIO()
df.info(buf=buffer)
s = buffer.getvalue()
with open("df_info.txt", "w",
encoding="utf-8") as f:
f.write(s)
Result:
Sample output:
column non-null count dtype
We should get the output like in result in above 3 columns.
How can I do this?
Solution
Use splitlines
for lists, then indexig for remove first 5 values and last 2 and split by space with DataFrame constructor:
import io
buffer = io.StringIO()
df.info(buf=buffer)
lines = buffer.getvalue().splitlines()
df = (pd.DataFrame([x.split() for x in lines[5:-2]], columns=lines[3].split())
.drop('Count',axis=1)
.rename(columns={'Non-Null':'Non-Null Count'}))
print (df)
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.