dexter04
dexter04

Reputation: 123

Creating variable number of lists from pandas dataframes

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

Upvotes: 0

Views: 454

Answers (2)

jezrael
jezrael

Reputation: 863186

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']

Upvotes: 2

robotrovsky
robotrovsky

Reputation: 124

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__())

Upvotes: -1

Related Questions