0

I have a pandas dataframe being generated by some other piece of code - the dataframe may have different number of columns each time it is generated: let's call them col1,col2,...,coln where n is not fixed. Please note that col1,col2,... are just placeholders, the actual names of columns can be arbitrary like TimeStamp or PrevState.

From this, I want to convert each column into a list, with the name of the list being the same as the column. So, I want a list named col1 with the entries in the first column of the dataframe and so on till coln.

How do I do this?

Thanks

2
  • post the sample dataframe and expected output Commented Sep 3, 2020 at 8:37
  • Don't dynamically create variables, use a container. List another list, or in this case, a dict. Commented Sep 3, 2020 at 9:46

2 Answers 2

2

It is not recommended, better is create dictionary:

d = df.to_dict('list')

And then select list by keys of dict from columns names:

print (d['col'])

Sample:

df = pd.DataFrame({
        'A':list('abcdef'),
        'B':[4,5,4,5,5,4],
        'C':[7,8,9,4,2,3],
})

d = df.to_dict('list')
print (d)
{'A': ['a', 'b', 'c', 'd', 'e', 'f'], 'B': [4, 5, 4, 5, 5, 4], 'C': [7, 8, 9, 4, 2, 3]}

print (d['A'])
['a', 'b', 'c', 'd', 'e', 'f']
Sign up to request clarification or add additional context in comments.

2 Comments

explicit is better than implicit, use df.to_dict('list')
@juanpa.arrivillaga - ya, btw, reopened, because part dupe.
-1
import pandas as pd

df = pd.DataFrame()
df["col1"] = [1,2,3,4,5]
df["colTWO"] = [6,7,8,9,10]

for col_name in df.columns:
    exec(col_name + " = " + df[col_name].values.__repr__())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.