Index de l'article

Get dataframe from lists

One list, one field

import pandas as pd
from tabulate import tabulate
 
print('\nONE LIST, ONE FIELD')
MyList = ['7878', 'Bbbb', 'azerfg', '545', 'XXX']
df = pd.DataFrame(MyList)
df.columns = ['My field']
number = df.shape[0]
 
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=False))
print(number)

Several lists in their own field, each list with same lenght

import pandas as pd
from tabulate import tabulate
 
print('\nSEVERAL LISTS IN THEIR OWN FIELD')
print('EACH LIST WITH SAME LENGHT')
MyList1 = ['7878', 'Bbbb', 'azerfg', '545', 'XXX']
MyList2 = ['Bruno', 'Constance', 'Mathieu', 'Abbes', 'Georges']
 
df = pd.DataFrame(
{'My field 1': MyList1,
'My field 2': MyList2
})
 
number = df.shape[0]
 
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=False))
print(number)

Several lists in the same field, lists with any lenght

import pandas as pd
from tabulate import tabulate
 
print('\nSEVERAL LISTS IN THE SAME FIELD')
print('LISTS WITH ANY LENGHT')
MyList1 = ['7878', 'Bbbb', 'azerfg', '545', 'XXX']
MyList2 = ['Bruno', 'Constance', 'Mathieu', 'Abbes']
 
df = pd.DataFrame(MyList1 + MyList2)
df.columns = ['My field']
number = df.shape[0]
 
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=False))
print(number)

Several lists from dictionaries, each list element as a field, lists with any lenght

import pandas as pd
from tabulate import tabulate
 
print('\nSEVERAL LISTS FROM DICTIONARIES')
print('EACH LIST ELEMENT AS A FIELD')
print('LISTS WITH ANY LENGHT')
MyList1 = ['Bruno', '11111', 'Rouge']
MyList2 = ['Constance', '22222', 'Jaune']
MyList3 = ['Mathieu', '33333', 'Bleu']
MyList4 = ['Abbes', '44444']
 
df = pd.DataFrame([MyList1] + [MyList2] + [MyList3] + [MyList4])
df.columns = ['My field 1', 'My field 2', 'My field 3']
number = df.shape[0]
 
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=False))
print(number)

A list from several lists from dictionaries, each sub-list as a row, each element from sub-list as a field, lists with any lenght

import pandas as pd
from tabulate import tabulate
 
print('\n LIST FROM SEVERAL LISTS FROM DICTIONARIES')
print('EACH SUB-LIST AS A ROW')
print('EACH ELEMENT FROM SUB-LIST AS A FIELD')
print('LISTS WITH ANY LENGHT')
 
MyList = [
    ['Bruno', '11111', 'Rouge'],
    ['Constance', '22222', 'Jaune'],
    ['Mathieu', '33333', 'Bleu'],
    ['Abbes', '44444']
]
 
df = pd.DataFrame(columns=['My field 1', 'My field 2', 'My field 3'], data=MyList)
 
number = df.shape[0]
 
print(tabulate(df.head(10), headers='keys', tablefmt='psql', showindex=False))
print(number)

 

Liens ou pièces jointes
Télécharger ce fichier (France-Departements-Deformation.zip)France-Departements-Deformation.zip[France-Departements-Deformation]335 Ko
Télécharger ce fichier (simple_countries.zip)simple_countries.zip[simple_countries]1880 Ko