Home > Mobile >  transform a lits of lists into a dataframe
transform a lits of lists into a dataframe

Time:10-13

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.

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

Any ideas?

CodePudding user response:

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

CodePudding user response:

There are numerous examples of converting LoL to dataframes out there:

https://www.geeksforgeeks.org/creating-pandas-dataframe-using-list-of-lists/

https://www.tutorialspoint.com/how-to-create-a-pandas-dataframe-using-a-list-of-lists

  • Related