[python] Use DataFrame to label arbitrary variables and arrays together and save them in csv [pandas] Re-UP

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

Content analysis of bugs

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

[python] Use DataFrame to label arbitrary variables and arrays together and save them in csv [pandas] Re-UP
[python] Use DataFrame to label arbitrary variables and arrays together and save them in csv [pandas]
How to use is and == in Python
Use os.getenv to get environment variables in Python
I tried to summarize how to use pandas in python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Put the lists together in pandas to make a DataFrame
Display numbers and letters assigned to variables in python print
Collect tweets using tweepy in Python and save them in MongoDB
Comparison of how to use higher-order functions in Python 2 and 3
processing to use notMNIST data in Python (and tried to classify it)
[Python] How to add rows and columns to a table (pandas DataFrame)
How to use functions in separate files Perl and Python versions
How to use Serverless Framework & Python environment variables and manage stages
[Python] When you want to use all variables in another file
[Python3] Save the mean and covariance matrix in json with pandas
I was addicted to confusing class variables and instance variables in Python
[Python] How to use Pandas Series
How to use SQLite in Python
[Introduction to Python] Let's use pandas
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
How to get a specific column name and index name in pandas DataFrame
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
Connect to postgreSQL from Python and use stored procedures in a loop.
Read CSV file with Python and convert it to DataFrame as it is
[Python] What is pandas Series and DataFrame?
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to split and save a DataFrame
[Python] Use pandas to extract △△ that maximizes ○○
How to access environment variables in Python
[Python] Use and and or when creating variables
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]
Easy way to use Wikipedia in Python
How to reassign index in pandas dataframe
[Python] Pandas to fully understand in 10 minutes
To reference environment variables in Python in Blender
Read Python csv and export to txt
python: How to use locals () and globals ()
How to use __slots__ in Python class
[Python] Add total rows to Pandas DataFrame
How to use Python zip and enumerate
How to use regular expressions in Python
How to read CSV files in Pandas
Adding Series to columns in python pandas
How to use pandas Timestamp and date_range
Tips for those who are wondering how to use is and == in Python
[Python] How to name table data and output it in csv (to_csv method)
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
[Python learning part 3] Convert pandas DataFrame, Series, and standard List to each other
Save Twitter's tweets with Geo in CSV and plot them on Google Map.