0

Is there a method to create column names based on the input variables in a Dataframe? Currently I am manually adjusting the column names whenever I make a change to the input variable. It would benefit me to only do this step once.

An example would be:

data = []
data1 = [1, 2, 3]
data2 = [3, 4, 5]
data3 = [6, 'a', 12]
combined = [data1, data2, data3]
data.append = (combined)

df = pd.Dataframe(data, columns = combined)

Desired result:

data1 data2 data3
0 1 3 6
1 2 4 a
2 3 5 12
rpanai
  • 12,515
  • 2
  • 42
  • 64
Frits
  • 113
  • 1
  • 8
  • 2
    You can define variables as key-value pairs inside a dictionary, this way you can keep track of all the variables more efficiently plus creating a dataframe from such dictionary is very straightforward task. – Shubham Sharma Apr 28 '21 at 11:16

1 Answers1

0

The problem is python doesn't have an in built function for turning the name of your variable into a string.

A previous question and answer about getting names offers a function for enquiring through global objects to find the name of a given variable.
Alternatively you can just write a list of strings containing the names (see commented-out lines).

data1 = [1, 2, 3]
data2 = [3, 4, 5]
data3 = [6, 'a', 12]
combined = [data1, data2, data3]
# column_names = ["data1", "data2", "data3"] 

def name_of_global_obj(xx):
    for objname, oid in globals().items():
        if oid is xx:
            return objname

df = pd.DataFrame({name_of_global_obj(data_i) : data_i for data_i in combined})
# df = pd.DataFrame({name : data_i for name, data in zip(column_names,combined)})
user213305
  • 402
  • 2
  • 10