Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

transform a lits of lists into a dataframe

I have the following list of lists:

list1=[['remote attackers',
  'attacker',
  'malicious code',
  'malicious user',
  'attack'],
 ['greenbone vulnerability manager',
  'web application abuses',
  'web management interfaces',
  'application'],
 ['ftp servers', 'server', 'tcp', 'application', 'rpc']]

I want to cast this list of lists into a dataframe:

clusters
0 remote attackers,attacker,malicious code,malicious user,attack
1 greenbone vulnerability manager, web application abuses, web management interfaces, application
2 ftp servers,server,tcp,application,rpc

I tried to flat the list but this did not output the expected outcome.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Also, I tried to iterate over the list of lists and append each list into a dataframe but this is not working.

Any ideas?

>Solution :

Simply use the DataFrame constructor:

df = pd.DataFrame({'clusters': list1})

output:

                                            clusters
0  [remote attackers, attacker, malicious code, m...
1  [greenbone vulnerability manager, web applicat...
2       [ftp servers, server, tcp, application, rpc]

If you want concatenated strings:

df = pd.DataFrame({'clusters': map(','.join, list1)})

output:

                                            clusters
0  remote attackers,attacker,malicious code,malic...
1  greenbone vulnerability manager,web applicatio...
2             ftp servers,server,tcp,application,rpc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading