0

How to create a Dataframe from a list of variables corresponding to its value.

A = 10
B = 15
df = [A,B]

Output = [output]

1

starkt964
  • 13
  • 4
  • 1
    See [this question and answers for the difficulties in trying to access the name of a variable in python](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string). If you control how the variables are being assigned in the first place, then save them in a dict instead (`{'a':10,'b':5}`) as those play nice with pandas. If not, then weigh the time investment in implementing one of the related answers vs just retyping the entries for `col1` – G. Anderson May 02 '22 at 23:15

2 Answers2

0

for your desired output:-

list1 = [a, b]
list2 = ['A', 'B']    
pd.DataFrame(list(zip(list1, list2)), columns = ['Col1', 'Col2'])
Arun Kumar Khattri
  • 1,519
  • 15
  • 25
0

I think this is the closest to answering your question:

import pandas as pd

before = locals().copy()

A = 10
B = 15

after = locals().copy()

diff = {key: val for key, val in after.items() if key not in before and key not in ['after', 'before']}

df = pd.DataFrame(diff.items())
print(df)
Robert Haas
  • 615
  • 2
  • 8