l3g10n3
l3g10n3

Reputation: 81

Fastest way to "unpack' a pandas dataframe

Hope the title is not misleading. I load an Excel file in a pandas dataframe as usual

df = pd.read_excel('complete.xlsx')

and this is what's inside (usually is already ordered - this is a really small sample)

df
Out[21]: 
    Country       City First Name  Last Name  Ref
0   England     London       John      Smith   34
1   England     London       Bill       Owen  332
2   England   Brighton        Max      Crowe   25
3   England   Brighton      Steve      Grant   55
4    France      Paris     Roland      Tomas   44
5    France      Paris    Anatole     Donnet  534
6    France       Lyon     Paulin     Botrel  234
7     Spain     Madrid      Oriol  Abarquero   34
8     Spain     Madrid    Alberto    Olloqui  534
9     Spain  Barcelona      Ander     Moreno  254
10    Spain  Barcelona      Cesar     Aranda  222

what I need to do is automating an export of the data creating a sqlite db for every country, (i.e. 'England.sqlite') which will contain a table for evey city (i.e. London and Brighton) and every table will have the related personnel info.

The sqlite is not a problem, I'm only trying to figure how to "unpack" the dataframe in the most rapid and "pythonic way

Thanks

Upvotes: 2

Views: 67

Answers (1)

jezrael
jezrael

Reputation: 862691

You can loop by DataFrame.groupby object:

for i, subdf in df.groupby('Country'):
     print (i)
     print (subdf)
     #processing

Upvotes: 2

Related Questions