I wanted to save variables all at once, so I made such a program. It can be saved by applying a two-dimensional array.
... I noticed after writing, but the method of adding may be better [If you are busy, jump to the postscript](# postscript) If you simply want to save all variables, use dill
With the append method, an error occurred in the type relationship or the data you tried to use. I didn't get an error when using DataFrame, so It may not be meaningless to insert a DataFrame once.
import pandas as pd
First import pandas
# ---Define variables for testing---
a ,b,c,d = 1,2,3,4
xx = [3,6,8]
yy = [5,8,2]
zz = [8,2,8]
Save this function, array
# ---Collect variables into dataframe and save csv---
def getName(obj):
return [k for k, v in globals().items() if id(obj) == id(v)][0] #Return variable name with str
Get the variable name as str by using this function It is almost a copy of this article Click here for details
df = pd.DataFrame()
datas1 = [xx,yy,zz] #While using the variable name as a header, put together a one-dimensional array into a dataframe
for data in datas1:
df = pd.concat([df, pd.DataFrame({getName(data):data})],axis=1) # {}Add dataframe with concat using set
Create a DataFrame based on {name: value}
and save more and more to df
with concat.
By inserting axis = 1, it will be added horizontally
datas2 = [a,b,c,d]#While using the variable name as a header, group the variables into a dataframe
for data in datas2:
df = pd.concat([df, pd.DataFrame({getName(data):[data]})],axis=1)# {}Add dataframe with concat using set
Do the same thing as before
The value
of{name: value}
must be an array, so the value
is [data]
df.to_csv('test.csv')
print(df)
del df
Save and display.
Since df
tends to be used in various places, I will delete it for the time being.
You can see that it is displayed and saved properly in the table below.
xx yy zz a b c d
0 3 5 8 1.0 2.0 3.0 4.0
1 6 8 2 NaN NaN NaN NaN
2 8 2 8 NaN NaN NaN NaN
Source code
import pandas as pd
# ---Define variables for testing---
a ,b,c,d = 1,2,3,4
xx = [3,6,8]
yy = [5,8,2]
zz = [8,2,8]
# ---Collect variables into dataframe and save csv---
def getName(obj):
return [k for k, v in globals().items() if id(obj) == id(v)][0] #Return variable name with str
df = pd.DataFrame()
datas1 = [xx,yy,zz] #While using the variable name as a header, put together a one-dimensional array into a dataframe
for data in datas1:
df = pd.concat([df, pd.DataFrame({getName(data):data})],axis=1) # {}Add dataframe with concat using set
datas2 = [a,b,c,d]#While using the variable name as a header, group the variables into a dataframe
for data in datas2:
df = pd.concat([df, pd.DataFrame({getName(data):[data]})],axis=1)# {}Add dataframe with concat using set
df.to_csv('test.csv')
print(df)
del df
Write directly to csv
# ---Define variables for testing---
a ,b,c,d = 1,2,3,4
xx = [3,6,8]
yy = [5,8,2]
zz = [8,2,8]
import csv
# ---Select a variable and save csv---
def getName(obj):
return [k for k, v in globals().items() if id(obj) == id(v)][0] #Return variable name with str
with open('test2.csv','a', newline="") as f:
datas = [xx,yy,zz,]
writer = csv.writer(f)
for data in datas:
writer.writerow([getName(data)]+data)
datas = [a,b,c,d]
for data in datas:
writer.writerow([getName(data)]+[data])
Case division is to convert a single variable to list type
Output test2.csv
xx,3,6,8
yy,5,8,2
zz,8,2,8
a,1
b,2
c,3
d,4
Save
import dill
dill.dump_session('session.pkl')
Read
import dill
dill.load_session('session.pkl')
All variables can be used as they are.
Recommended Posts