0

I have a dataframe with 3 columns. I would like to create multiple dataframes based on the value of column StoreCountryName. An example of the dataframe dataset can be seem below.

+----------------------+--+------------------+--+------------------+
|     ReceiptCode      |  | ItemCategoryName |  | StoreCountryName |
+----------------------+--+------------------+--+------------------+
|  0000P70322000039467 |  |  Wrapping        |  |   Japan          |
|  0000P70322000031468 |  |  Kitchen         |  |   Germany        |
|  0000P70322000031467 |  |  Food            |  |   Germany        |
|  0000P70322000032267 |  |  Kitchen         |  |   Japan          |
|  0000P70322000031467 |  |  Food            |  |   Denmark        |
|  0000P70322000031867 |  |  Food            |  |   Denmark        |
|  0000P70322000051467 |  |  Interior        |  |   Germany        |
|  0000P70322000087468 |  |  Kitchen         |  |   Switzerland    |
|  0000P70322000031469 |  |  Leisure         |  |   Germany        |
|  0000P70322000031439 |  |  Food            |  |   Switzerland    |
+----------------------+--+------------------+--+------------------+

The result I would like for each StoreCountryName value is

print(dataset_Denmark)

    +----------------------+--+------------------+--+------------------+
    |     ReceiptCode      |  | ItemCategoryName |  | StoreCountryName |
    +----------------------+--+------------------+--+------------------+
    |  0000P70322000031467 |  |  Food            |  |   Denmark        |
    |  0000P70322000031867 |  |  Food            |  |   Denmark        |
    +----------------------+--+------------------+--+------------------+

Also, would it be possible to automatically name the dataframe as dataset+'_Country', i.e. dataset_Denmark?

3
  • The second half of your request is somewhat unreasonable because you would be better storing the results in a dictionary. The first part of your request is fundamental to pandas and I assume that you tried something before asking, but we can't see it. Commented Mar 22, 2019 at 14:07
  • 2
    You store them into a dict , check with{ x : y for x, y df.groupby('StoreCountryName')} Commented Mar 22, 2019 at 14:07
  • Got it - thank you. For the first part of the question, I split them manually. But I was just looking for a smarter way. Commented Mar 22, 2019 at 14:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.