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 used [python] DataFrame to label arbitrary variables and arrays together and save them in csv [pandas].
If you incorporate it into the program in production instead of the test program
There was a bug that all the names were saved as " data "
, so I will write a workaround.
The cause is unknown, but the detailed symptoms are summarized in [here](#Bug content analysis).
Above
Because the data was set to for data in datas:
in the for part
It seems that all the names have been saved as " data "
.
So you can specify it in the form of datas [i]
.
It is a mystery why it can be done with test code but not in production. Because it's in a function? ??
It's not a big rewrite, so I'll just leave the 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()
datas = [xx,yy,zz] #While using the variable name as a header, put together a one-dimensional array into a dataframe
for i in range(len(datas)):
df = pd.concat([df, pd.DataFrame({getName(datas[i]):datas[i]})],axis=1) # {}Add dataframe with concat using set
datas = [a,b,c,d]#While using the variable name as a header, group the variables into a dataframe
for i in range(len(datas)):
df = pd.concat([df, pd.DataFrame({getName(datas[i]):[datas[i]]})],axis=1)# {}Add dataframe with concat using set
df.to_csv('test.csv')
print(df)
del df
output
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
After turning the original program, changing the variable name and then turning it again, the above error occurred for some reason.
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
output
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
In this way, the first time it succeeds If you change the variable name again and turn it in this state
import pandas as pd
# ---Define variables for testing---
a ,b,c,d = 1,2,3,4
xxx= [3,6,8] ###Change this
yyy= [5,8,2] ###Change this
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()
###Change here
datas1 = [xxx,yyy,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
output
data data 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
The name will be data like this
Recommended Posts