Prompt:
Problem:
I have the following DataFrame:
    Col1  Col2  Col3  Type
0      1     2     3     1
1      4     5     6     1
2      7     8     9     2
3    10    11    12     2
4    13    14    15     3
5    16    17    18     3

The DataFrame is read from a CSV file. All rows which have Type 1 are on top, followed by the rows with Type 2, followed by the rows with Type 3, etc.
I would like to shuffle the order of the DataFrame's rows according to a list. \
For example, give a list [2, 4, 0, 3, 1, 5] and desired result should be:
    Col1  Col2  Col3  Type
2      7     8     9     2
4     13    14    15     3
0     1     2     3     1
3    10    11    12     2
1     4     5     6     1
5    16    17    18     3
...

How can I achieve this?

A:
<code>
import pandas as pd
import numpy as np

df = pd.DataFrame({'Col1': [1, 4, 7, 10, 13, 16],
                   'Col2': [2, 5, 8, 11, 14, 17],
                   'Col3': [3, 6, 9, 12, 15, 18],
                   'Type': [1, 1, 2, 2, 3, 3]})
List = np.random.permutation(len(df))
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df, List):
    return df.iloc[List]

result = g(df.copy(), List)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        data = data
        df, List = data
        return df.iloc[List]

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Col1": [1, 4, 7, 10, 13, 16],
                    "Col2": [2, 5, 8, 11, 14, 17],
                    "Col3": [3, 6, 9, 12, 15, 18],
                    "Type": [1, 1, 2, 2, 3, 3],
                }
            )
            List = np.random.permutation(len(df))
        return df, List

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df, List = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following DataFrame:
    Col1  Col2  Col3  Type
0      1     2     3     1
1      4     5     6     1
2      7     8     9     2
3    10    11    12     2
4    13    14    15     3
5    16    17    18     3

The DataFrame is read from a CSV file. All rows which have Type 1 are on top, followed by the rows with Type 2, followed by the rows with Type 3, etc.
I would like to shuffle the order of the DataFrame's rows according to a list.
For example, give a list [2, 4, 0, 3, 1, 5] and desired DataFrame should be:
    Col1  Col2  Col3  Type
2      7     8     9     2
4     13    14    15     3
0     1     2     3     1
3    10    11    12     2
1     4     5     6     1
5    16    17    18     3
...
I want to know how many rows have different Type than the original DataFrame. In this case, 4 rows (0,1,2,4) have different Type than origin.
How can I achieve this?

A:
<code>
import pandas as pd
import numpy as np

df = pd.DataFrame({'Col1': [1, 4, 7, 10, 13, 16],
                   'Col2': [2, 5, 8, 11, 14, 17],
                   'Col3': [3, 6, 9, 12, 15, 18],
                   'Type': [1, 1, 2, 2, 3, 3]})
List = np.random.permutation(len(df))
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df, List):
    df2 = df.iloc[List].reindex().reset_index(drop=True)
    return (df2.Type != df.Type).sum()

result = g(df.copy(), List)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        data = data
        df, List = data
        df2 = df.iloc[List].reindex().reset_index(drop=True)
        return (df2.Type != df.Type).sum()

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Col1": [1, 4, 7, 10, 13, 16],
                    "Col2": [2, 5, 8, 11, 14, 17],
                    "Col3": [3, 6, 9, 12, 15, 18],
                    "Type": [1, 1, 2, 2, 3, 3],
                }
            )
            List = np.random.permutation(len(df))
        return df, List

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        assert result == ans
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df, List = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have following pandas dataframe :

import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
              'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
              'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})

I'd like to change values in columns Qu1,Qu2,Qu3 according to value_counts() when value count great or equal 2
For example for Qu1 column
>>> pd.value_counts(data.Qu1) >= 2
cheese     True
potato     True
banana     True
apple     False
egg       False

I'd like to keep values cheese,potato,banana, because each value has at least two appearances.
From values apple and egg I'd like to create value others
For column Qu2 no changes :
>>> pd.value_counts(data.Qu2) >= 2
banana     True
apple      True
sausage    True

The final result as in attached test_data
test_data = DataFrame({'Qu1': ['other', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'other'],
                  'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                  'Qu3': ['other', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})

Thanks !

A:
<code>
import pandas as pd

df = pd.DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                   'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu2": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu2": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have following pandas dataframe :

import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
              'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
              'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})

I'd like to change values in columns Qu1,Qu2,Qu3 according to value_counts() when value count great or equal 3
For example for Qu1 column
>>> pd.value_counts(data.Qu1) >= 3
cheese     True
potato    False
banana    False
apple     False
egg       False

I'd like to keep values cheese, because each value has at least three appearances.
From values potato, banana, apple and egg I'd like to create value others
For column Qu2 no changes :
>>> pd.value_counts(data.Qu2) >= 3
banana     True
apple      True
sausage   False

The final result as in attached test_data
test_data = DataFrame({'Qu1': ['other', 'other', 'cheese', 'other', 'cheese', 'other', 'cheese', 'other', 'other'],
                  'Qu2': ['other', 'banana', 'apple', 'apple', 'apple', 'other', 'banana', 'banana', 'banana'],
                  'Qu3': ['other', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})

Thanks !

A:
<code>
import pandas as pd

df = pd.DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                   'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 3, "other")

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 3, "other")

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu2": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu2": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have following pandas dataframe :

import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
              'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
              'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})

I'd like to change values in columns Qu1,Qu2,Qu3 according to value_counts() when value count great or equal 2
For example for Qu1 column
>>> pd.value_counts(data.Qu1) >= 2
cheese     True
potato     True
banana     True
apple     False
egg       False

I'd like to keep values cheese,potato,banana, because each value has at least two appearances.
From values apple and egg I'd like to create value others
For column Qu2 no changes :
>>> pd.value_counts(data.Qu2) >= 2
banana     True
apple      True
sausage    True

The final result as in attached test_data
test_data = DataFrame({'Qu1': ['other', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'other'],
                  'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                  'Qu3': ['other', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})

Thanks !

A:
<code>
import pandas as pd

example_df = pd.DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                   'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
def f(df=example_df):
    # return the solution in this function
    # result = f(df)
    ### BEGIN SOLUTION

    result = df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")

    return result

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu2": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu2": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
def f(df):
[insert]
df = test_input
result = f(df)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have following pandas dataframe :

import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
              'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
              'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})

I'd like to change values in columns Qu1 according to value_counts() when value count great or equal 3 and change values in columns Qu2 and Qu3 according to value_counts() when value count great or equal 2.
For example for Qu1 column
>>> pd.value_counts(data.Qu1) >= 3
cheese     True
potato    False
banana    False
apple     False
egg       False

I'd like to keep values cheese, because each value has at least three appearances.
From values potato, banana, apple and egg I'd like to create value others
For column Qu2 no changes :
>>> pd.value_counts(data.Qu2) >= 2
banana     True
apple      True
sausage   True

The final result as in attached test_data
test_data = DataFrame({'Qu1': ['other', 'other', 'cheese', 'other', 'cheese', 'other', 'cheese', 'other', 'other'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                  'Qu3': ['other', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})

Thanks !

A:
<code>
import pandas as pd

df = pd.DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                   'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    for col in df.columns:
        vc = df[col].value_counts()
        if col == 'Qu1':
            df[col] = df[col].apply(lambda x: x if vc[x] >= 3 else 'other')
        else:
            df[col] = df[col].apply(lambda x: x if vc[x] >= 2 else 'other')
    return df

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        for col in df.columns:
            vc = df[col].value_counts()
            if col == "Qu1":
                df[col] = df[col].apply(lambda x: x if vc[x] >= 3 else "other")
            else:
                df[col] = df[col].apply(lambda x: x if vc[x] >= 2 else "other")
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu2": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu2": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have following pandas dataframe :

import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
              'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
              'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})

I'd like to change values in columns Qu1 according to value_counts() when value count great or equal 3 and change values in columns Qu2 and Qu3 according to value_counts() when value count great or equal 2.
For example for Qu1 column
>>> pd.value_counts(data.Qu1) >= 3
cheese     True
potato    False
banana    False
apple     False
egg       False

I'd like to keep values cheese because each value has at least three appearances.
From values potato, banana, apple and egg I'd like to create value others
However I want to reserve all the 'apple'. That means don't replace 'apple' with 'other' and only 'egg' should be replaced.
For column Qu2 no changes :
>>> pd.value_counts(data.Qu2) >= 2
banana     True
apple      True
sausage   True

The final result as in attached test_data
test_data = DataFrame({'Qu1': ['apple', 'other', 'cheese', 'other', 'cheese', 'other', 'cheese', 'other', 'other'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                  'Qu3': ['apple', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})

Thanks !

A:
<code>
import pandas as pd

df = pd.DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
                   'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
                   'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    for col in df.columns:
        vc = df[col].value_counts()
        if col == 'Qu1':
            df[col] = df[col].apply(lambda x: x if vc[x] >= 3 or x == 'apple' else 'other')
        else:
            df[col] = df[col].apply(lambda x: x if vc[x] >= 2 or x == 'apple' else 'other')
    return df

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        for col in df.columns:
            vc = df[col].value_counts()
            if col == "Qu1":
                df[col] = df[col].apply(
                    lambda x: x if vc[x] >= 3 or x == "apple" else "other"
                )
            else:
                df[col] = df[col].apply(
                    lambda x: x if vc[x] >= 2 or x == "apple" else "other"
                )
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu2": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "Qu1": [
                        "sausage",
                        "banana",
                        "apple",
                        "apple",
                        "apple",
                        "sausage",
                        "banana",
                        "banana",
                        "banana",
                    ],
                    "Qu2": [
                        "apple",
                        "potato",
                        "sausage",
                        "cheese",
                        "cheese",
                        "potato",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                    "Qu3": [
                        "apple",
                        "potato",
                        "cheese",
                        "banana",
                        "cheese",
                        "banana",
                        "cheese",
                        "potato",
                        "egg",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a dataset :
id    url     keep_if_dup
1     A.com   Yes
2     A.com   Yes
3     B.com   No
4     B.com   No
5     C.com   No

I want to remove duplicates, i.e. keep first occurence of "url" field, BUT  keep duplicates if the field "keep_if_dup" is YES.
Expected output :
id    url     keep_if_dup
1     A.com   Yes
2     A.com   Yes
3     B.com   No
5     C.com   No

What I tried :
Dataframe=Dataframe.drop_duplicates(subset='url', keep='first')

which of course does not take into account "keep_if_dup" field. Output is :
id    url     keep_if_dup
1     A.com   Yes
3     B.com   No
5     C.com   No

A:
<code>
import pandas as pd

df = pd.DataFrame({'url': ['A.com', 'A.com', 'A.com', 'B.com', 'B.com', 'C.com', 'B.com'],
                   'keep_if_dup': ['Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    return df.loc[(df['keep_if_dup'] =='Yes') | ~df['url'].duplicated()]

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.loc[(df["keep_if_dup"] == "Yes") | ~df["url"].duplicated()]

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "url": [
                        "A.com",
                        "A.com",
                        "A.com",
                        "B.com",
                        "B.com",
                        "C.com",
                        "B.com",
                    ],
                    "keep_if_dup": ["Yes", "Yes", "No", "No", "No", "No", "Yes"],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a dataset :
id    url     drop_if_dup
1     A.com   Yes
2     A.com   Yes
3     B.com   No
4     B.com   No
5     C.com   No

I want to remove duplicates, i.e. keep first occurence of "url" field, BUT keep duplicates if the field "drop_if_dup" is No.
Expected output :
id    url     drop_if_dup
1     A.com   Yes
3     B.com   No
4     B.com   No
5     C.com   No

What I tried :
Dataframe=Dataframe.drop_duplicates(subset='url', keep='first')

which of course does not take into account "drop_if_dup" field. Output is :
id    url     drop_if_dup
1     A.com   Yes
3     B.com   No
5     C.com   No

A:
<code>
import pandas as pd

df = pd.DataFrame({'url': ['A.com', 'A.com', 'A.com', 'B.com', 'B.com', 'C.com', 'B.com'],
                   'drop_if_dup': ['Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    return df.loc[(df['drop_if_dup'] =='No') | ~df['url'].duplicated()]

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.loc[(df["drop_if_dup"] == "No") | ~df["url"].duplicated()]

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "url": [
                        "A.com",
                        "A.com",
                        "A.com",
                        "B.com",
                        "B.com",
                        "C.com",
                        "B.com",
                    ],
                    "drop_if_dup": ["Yes", "Yes", "No", "No", "No", "No", "Yes"],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a dataset :
id    url     keep_if_dup
1     A.com   Yes
2     A.com   Yes
3     B.com   No
4     B.com   No
5     C.com   No

I want to remove duplicates, i.e. keep last occurence of "url" field, BUT keep duplicates if the field "keep_if_dup" is YES.
Expected output :
id    url     keep_if_dup
1     A.com   Yes
2     A.com   Yes
4     B.com   No
5     C.com   No

What I tried :
Dataframe=Dataframe.drop_duplicates(subset='url', keep='first')

which of course does not take into account "keep_if_dup" field. Output is :
id    url     keep_if_dup
1     A.com   Yes
3     B.com   No
5     C.com   No

A:
<code>
import pandas as pd

df = pd.DataFrame({'url': ['A.com', 'A.com', 'A.com', 'B.com', 'B.com', 'C.com', 'B.com'],
                   'keep_if_dup': ['Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    return df.loc[(df['keep_if_dup'] =='Yes') | ~df['url'].duplicated(keep='last')]

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        return df.loc[(df["keep_if_dup"] == "Yes") | ~df["url"].duplicated(keep="last")]

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "url": [
                        "A.com",
                        "A.com",
                        "A.com",
                        "B.com",
                        "B.com",
                        "C.com",
                        "B.com",
                    ],
                    "keep_if_dup": ["Yes", "Yes", "No", "No", "No", "No", "Yes"],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm Looking for a generic way of turning a DataFrame to a nested dictionary
This is a sample data frame
    name    v1  v2  v3
0   A       A1  A11 1
1   A       A2  A12 2
2   B       B1  B12 3
3   C       C1  C11 4
4   B       B2  B21 5
5   A       A2  A21 6

The number of columns may differ and so does the column names.
like this :
{
'A' : {
    'A1' : { 'A11' : 1 }
    'A2' : { 'A12' : 2 , 'A21' : 6 }} ,
'B' : {
    'B1' : { 'B12' : 3 } } ,
'C' : {
    'C1' : { 'C11' : 4}}
}

What is best way to achieve this ?
closest I got was with the zip function but haven't managed to make it work for more then one level (two columns).

A:
<code>
import pandas as pd

df = pd.DataFrame({'name': ['A', 'A', 'B', 'C', 'B', 'A'],
                   'v1': ['A1', 'A2', 'B1', 'C1', 'B2', 'A2'],
                   'v2': ['A11', 'A12', 'B12', 'C11', 'B21', 'A21'],
                   'v3': [1, 2, 3, 4, 5, 6]})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    if len(df.columns) == 1:
        if df.values.size == 1: return df.values[0][0]
        return df.values.squeeze()
    grouped = df.groupby(df.columns[0])
    d = {k: g(t.iloc[:, 1:]) for k, t in grouped}
    return d

result = g(df.copy())

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        if len(df.columns) == 1:
            if df.values.size == 1:
                return df.values[0][0]
            return df.values.squeeze()
        grouped = df.groupby(df.columns[0])
        d = {k: generate_ans(t.iloc[:, 1:]) for k, t in grouped}
        return d

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "name": ["A", "A", "B", "C", "B", "A"],
                    "v1": ["A1", "A2", "B1", "C1", "B2", "A2"],
                    "v2": ["A11", "A12", "B12", "C11", "B21", "A21"],
                    "v3": [1, 2, 3, 4, 5, 6],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        assert result == ans
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

Can I export pandas DataFrame to Excel stripping tzinfo?

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

Actual output

2015-12-01 00:00:00-06:00

Desired output
2015-12-01 00:00:00

I have tried to get the characters I want using the str() method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and to use the str() method to get the characters I want.
Is there an easier solution?

A:
<code>
import pandas as pd

df = pd.DataFrame({'datetime': ['2015-12-01 00:00:00-06:00', '2015-12-02 00:01:00-06:00', '2015-12-03 00:00:00-06:00']})
df['datetime'] = pd.to_datetime(df['datetime'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy
import tokenize, io

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["datetime"] = df["datetime"].dt.tz_localize(None)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2015-12-01 00:00:00-06:00",
                        "2015-12-02 00:01:00-06:00",
                        "2015-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        elif test_case_id == 2:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2016-12-02 00:01:00-06:00",
                        "2016-12-01 00:00:00-06:00",
                        "2016-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "tz_localize" in tokens


Prompt:
Problem:
I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

Can I export pandas DataFrame to Excel stripping tzinfo?

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

Actual output

2015-12-01 00:00:00-06:00

Desired output
2015-12-01 00:00:00

I have tried to get the characters I want using the str() method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and to use the str() method to get the characters I want.
Is there an easier solution?

A:
<code>
import pandas as pd

example_df = pd.DataFrame({'datetime': ['2015-12-01 00:00:00-06:00', '2015-12-02 00:01:00-06:00', '2015-12-03 00:00:00-06:00']})
example_df['datetime'] = pd.to_datetime(example_df['datetime'])
def f(df=example_df):
    # return the solution in this function
    # result = f(df)
    ### BEGIN SOLUTION

    df['datetime'] = df['datetime'].dt.tz_localize(None)
    result = df

    return result

import pandas as pd
import numpy as np
import copy
import tokenize, io

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["datetime"] = df["datetime"].dt.tz_localize(None)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2015-12-01 00:00:00-06:00",
                        "2015-12-02 00:01:00-06:00",
                        "2015-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        elif test_case_id == 2:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2016-12-02 00:01:00-06:00",
                        "2016-12-01 00:00:00-06:00",
                        "2016-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
def f(df):
[insert]
df = test_input
result = f(df)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "tz_localize" in tokens


Prompt:
Problem:
I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

Can I export pandas DataFrame to Excel stripping tzinfo?

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

Actual output

2015-12-01 00:00:00-06:00

Desired output
01-Dec-2015 00:00:00

I have tried to get the characters I want using the str() method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and to use the str() method to get the characters I want.
Then I want the 'datetime' to go from smallest to largest and let 'datetime' look like this format: 19-May-2016 13:50:00.
Is there an easier solution?

A:
<code>
import pandas as pd

df = pd.DataFrame({'datetime': ['2015-12-01 00:00:00-06:00', '2015-12-02 00:01:00-06:00', '2015-12-03 00:00:00-06:00']})
df['datetime'] = pd.to_datetime(df['datetime'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

df['datetime'] = df['datetime'].dt.tz_localize(None)
df.sort_values(by='datetime', inplace=True)
df['datetime'] = df['datetime'].dt.strftime('%d-%b-%Y %T')

import pandas as pd
import numpy as np
import copy
import tokenize, io

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["datetime"] = df["datetime"].dt.tz_localize(None)
        df.sort_values(by="datetime", inplace=True)
        df["datetime"] = df["datetime"].dt.strftime("%d-%b-%Y %T")
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2015-12-01 00:00:00-06:00",
                        "2015-12-02 00:01:00-06:00",
                        "2015-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        elif test_case_id == 2:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2016-12-02 00:01:00-06:00",
                        "2016-12-01 00:00:00-06:00",
                        "2016-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "tz_localize" in tokens


Prompt:
Problem:
I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

Can I export pandas DataFrame to Excel stripping tzinfo?

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

Actual output

2015-12-01 00:00:00-06:00

Desired output
2015-12-01 00:00:00

I have tried to get the characters I want using the str() method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and to use the str() method to get the characters I want.
Then I want the 'datetime' to go from smallest to largest.
Is there an easier solution?

A:
<code>
import pandas as pd

df = pd.DataFrame({'datetime': ['2015-12-01 00:00:00-06:00', '2015-12-02 00:01:00-06:00', '2015-12-03 00:00:00-06:00']})
df['datetime'] = pd.to_datetime(df['datetime'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

def g(df):
    df['datetime'] = df['datetime'].dt.tz_localize(None)
    df.sort_values(by='datetime', inplace=True)
    return df

df = g(df.copy())

import pandas as pd
import numpy as np
import copy
import tokenize, io

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["datetime"] = df["datetime"].dt.tz_localize(None)
        df.sort_values(by="datetime", inplace=True)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2015-12-01 00:00:00-06:00",
                        "2015-12-02 00:01:00-06:00",
                        "2015-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        elif test_case_id == 2:
            df = pd.DataFrame(
                {
                    "datetime": [
                        "2016-12-02 00:01:00-06:00",
                        "2016-12-01 00:00:00-06:00",
                        "2016-12-03 00:00:00-06:00",
                    ]
                }
            )
            df["datetime"] = pd.to_datetime(df["datetime"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "tz_localize" in tokens


Prompt:
Problem:
I have a data set like below:
name    status    number   message
matt    active    12345    [job:  , money: none, wife: none]
james   active    23456    [group: band, wife: yes, money: 10000]
adam    inactive  34567    [job: none, money: none, wife:  , kids: one, group: jail]

How can I extract the key value pairs, and turn them into a dataframe expanded all the way out?

Expected output:
name    status   number    job    money    wife    group   kids
matt    active   12345     none   none     none    none    none
james   active   23456     none   10000    none    band    none
adam    inactive 34567     none   none     none    none    one

Notice: 'none' is a string
The message contains multiple different key types.
Any help would be greatly appreciated.

A:
<code>
import pandas as pd

df = pd.DataFrame({'name': ['matt', 'james', 'adam'],
                   'status': ['active', 'active', 'inactive'],
                   'number': [12345, 23456, 34567],
                   'message': ['[job:  , money: none, wife: none]',
                               '[group: band, wife: yes, money: 10000]',
                               '[job: none, money: none, wife:  , kids: one, group: jail]']})
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import yaml
def g(df):
    df.message = df.message.replace(['\[','\]'],['{','}'], regex=True).apply(yaml.safe_load)
    df1 = pd.DataFrame(df.pop('message').values.tolist(), index=df.index)
    result = pd.concat([df, df1], axis=1)
    result = result.replace('', 'none')
    result = result.replace(np.nan, 'none')
    return result

result = g(df.copy())

import pandas as pd
import numpy as np
import yaml
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df.message = df.message.replace(["\[", "\]"], ["{", "}"], regex=True).apply(
            yaml.safe_load
        )
        df1 = pd.DataFrame(df.pop("message").values.tolist(), index=df.index)
        result = pd.concat([df, df1], axis=1)
        result = result.replace("", "none")
        result = result.replace(np.nan, "none")
        return result

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "name": ["matt", "james", "adam"],
                    "status": ["active", "active", "inactive"],
                    "number": [12345, 23456, 34567],
                    "message": [
                        "[job:  , money: none, wife: none]",
                        "[group: band, wife: yes, money: 10000]",
                        "[job: none, money: none, wife:  , kids: one, group: jail]",
                    ],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "name": ["matt", "james", "adam"],
                    "status": ["active", "active", "inactive"],
                    "number": [12345, 23456, 34567],
                    "message": [
                        "[job:  , money: 114514, wife: none, kids: one, group: jail]",
                        "[group: band, wife: yes, money: 10000]",
                        "[job: none, money: none, wife:  , kids: one, group: jail]",
                    ],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a dataframe that looks like this:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  0.420455
4    1069105  0.414603
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

what I'm trying to achieve is to multiply certain score values corresponding to specific products by a constant.
I have the products target of this multiplication in a list like this: [1069104, 1069105] (this is just a simplified
example, in reality it would be more than two products) and my goal is to obtain this:
Multiply scores corresponding to products 1069104 and 1069105 by 10:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  4.204550
4    1069105  4.146030
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

I know that exists DataFrame.multiply but checking the examples it works for full columns, and I just one to change those specific values.

A:
<code>
import pandas as pd

df = pd.DataFrame({'product': [1179160, 1066490, 1148126, 1069104, 1069105, 1160330, 1069098, 1077784, 1193369, 1179741],
                   'score': [0.424654, 0.424509, 0.422207, 0.420455, 0.414603, 0.168784, 0.168749, 0.168738, 0.168703, 0.168684]})
products = [1066490, 1077784]
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

df.loc[df['product'].isin(products), 'score'] *= 10


Prompt:
Problem:
I have a dataframe that looks like this:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  0.420455
4    1069105  0.414603
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

what I'm trying to achieve is to multiply certain score values corresponding to specific products by a constant.
I have a list like this: [1069104, 1069105] (this is just a simplified
example, in reality it would be more than two products) and my goal is to obtain this:
Multiply scores not in the list by 10:
     product     score
0    1179160  4.24654
1    1066490  4.24509
2    1148126  4.22207
3    1069104  0.4204550
4    1069105  0.146030
..       ...       ...
491  1160330  1.68784
492  1069098  1.68749
493  1077784  1.68738
494  1193369  1.68703
495  1179741  1.68684

I know that exists DataFrame.multiply but checking the examples it works for full columns, and I just one to change those specific values.

A:
<code>
import pandas as pd

df = pd.DataFrame({'product': [1179160, 1066490, 1148126, 1069104, 1069105, 1160330, 1069098, 1077784, 1193369, 1179741],
                   'score': [0.424654, 0.424509, 0.422207, 0.420455, 0.414603, 0.168784, 0.168749, 0.168738, 0.168703, 0.168684]})
products = [1066490, 1077784]
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

df.loc[~df['product'].isin(products), 'score'] *= 10


Prompt:
Problem:
I have a dataframe that looks like this:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  0.420455
4    1069105  0.414603
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

what I'm trying to achieve is to multiply certain score values corresponding to specific products by a constant.
I have the products target of this multiplication in a list like this: [[1069104, 1069105], [1179159, 1179161]] (this is just a simplified
example, in reality it would be more than two products) and my goal is to obtain this:
Multiply scores corresponding to products which between [1069104, 1069105] or [1179159, 1179161] by 10:
     product     score
0    1179160  4.24654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  4.204550
4    1069105  4.146030
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

I know that exists DataFrame.multiply but checking the examples it works for full columns, and I just one to change those specific values.

A:
<code>
import pandas as pd

df = pd.DataFrame({'product': [1179160, 1066490, 1148126, 1069104, 1069105, 1160330, 1069098, 1077784, 1193369, 1179741],
                   'score': [0.424654, 0.424509, 0.422207, 0.420455, 0.414603, 0.168784, 0.168749, 0.168738, 0.168703, 0.168684]})
products = [[1069104, 1069105], [1066489, 1066491]]
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

for product in products:
    df.loc[(df['product'] >= product[0]) & (df['product'] <= product[1]), 'score'] *= 10


Prompt:
Problem:
I have a dataframe that looks like this:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  0.420455
4    1069105  0.414603
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

what I'm trying to achieve is to Min-Max Normalize certain score values corresponding to specific products.
I have a list like this: [1069104, 1069105] (this is just a simplified
example, in reality it would be more than two products) and my goal is to obtain this:
Min-Max Normalize scores corresponding to products 1069104 and 1069105:
     product     score
0    1179160  0.424654
1    1066490  0.424509
2    1148126  0.422207
3    1069104  1
4    1069105  0
..       ...       ...
491  1160330  0.168784
492  1069098  0.168749
493  1077784  0.168738
494  1193369  0.168703
495  1179741  0.168684

I know that exists DataFrame.multiply but checking the examples it works for full columns, and I just one to change those specific values.

A:
<code>
import pandas as pd

df = pd.DataFrame({'product': [1179160, 1066490, 1148126, 1069104, 1069105, 1160330, 1069098, 1077784, 1193369, 1179741],
                   'score': [0.424654, 0.424509, 0.422207, 0.420455, 0.414603, 0.168784, 0.168749, 0.168738, 0.168703, 0.168684]})
products = [1066490, 1077784, 1179741]
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

Max = df.loc[df['product'].isin(products), 'score'].max()
Min = df.loc[df['product'].isin(products), 'score'].min()
df.loc[df['product'].isin(products), 'score'] = (df.loc[df['product'].isin(products), 'score'] - Min) / (Max - Min)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        data = data
        df, prod_list = data
        Max = df.loc[df["product"].isin(prod_list), "score"].max()
        Min = df.loc[df["product"].isin(prod_list), "score"].min()
        df.loc[df["product"].isin(prod_list), "score"] = (
            df.loc[df["product"].isin(prod_list), "score"] - Min
        ) / (Max - Min)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "product": [
                        1179160,
                        1066490,
                        1148126,
                        1069104,
                        1069105,
                        1160330,
                        1069098,
                        1077784,
                        1193369,
                        1179741,
                    ],
                    "score": [
                        0.424654,
                        0.424509,
                        0.422207,
                        0.420455,
                        0.414603,
                        0.168784,
                        0.168749,
                        0.168738,
                        0.168703,
                        0.168684,
                    ],
                }
            )
            products = [1066490, 1077784, 1179741]
        return df, products

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df, products = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
Given a pandas DataFrame, how does one convert several binary columns (where 1 denotes the value exists, 0 denotes it doesn't) into a single categorical column?
Another way to think of this is how to perform the "reverse pd.get_dummies()"?
Here is an example of converting a categorical column into several binary columns:
import pandas as pd
s = pd.Series(list('ABCDAB'))
df = pd.get_dummies(s)
df
   A  B  C  D
0  1  0  0  0
1  0  1  0  0
2  0  0  1  0
3  0  0  0  1
4  1  0  0  0
5  0  1  0  0

What I would like to accomplish is given a dataframe
df1
   A  B  C  D
0  1  0  0  0
1  0  1  0  0
2  0  0  1  0
3  0  0  0  1
4  1  0  0  0
5  0  1  0  0

could do I convert it into
df1
   A  B  C  D   category
0  1  0  0  0   A
1  0  1  0  0   B
2  0  0  1  0   C
3  0  0  0  1   D
4  1  0  0  0   A
5  0  1  0  0   B

A:
<code>
import pandas as pd

df = pd.DataFrame({'A': [1, 0, 0, 0, 1, 0],
                   'B': [0, 1, 0, 0, 0, 1],
                   'C': [0, 0, 1, 0, 0, 0],
                   'D': [0, 0, 0, 1, 0, 0]})
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["category"] = df.idxmax(axis=1)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "A": [1, 0, 0, 0, 1, 0],
                    "B": [0, 1, 0, 0, 0, 1],
                    "C": [0, 0, 1, 0, 0, 0],
                    "D": [0, 0, 0, 1, 0, 0],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "A": [0, 0, 0, 1, 0, 0],
                    "B": [0, 0, 1, 0, 0, 0],
                    "C": [0, 1, 0, 0, 0, 1],
                    "D": [1, 0, 0, 0, 1, 0],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
Given a pandas DataFrame, how does one convert several binary columns (where 0 denotes the value exists, 1 denotes it doesn't) into a single categorical column?
Another way to think of this is how to perform the "reverse pd.get_dummies()"?

What I would like to accomplish is given a dataframe
df1
   A  B  C  D
0  0  1  1  1
1  1  0  1  1
2  1  1  0  1
3  1  1  1  0
4  0  1  1  1
5  1  0  1  1

could do I convert it into
df1
   A  B  C  D category
0  0  1  1  1        A
1  1  0  1  1        B
2  1  1  0  1        C
3  1  1  1  0        D
4  0  1  1  1        A
5  1  0  1  1        B

A:
<code>
import pandas as pd

df = pd.DataFrame({'A': [0, 1, 1, 1, 0, 1],
                   'B': [1, 0, 1, 1, 1, 0],
                   'C': [1, 1, 0, 1, 1, 1],
                   'D': [1, 1, 1, 0, 1, 1]})
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["category"] = df.idxmin(axis=1)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "A": [0, 1, 1, 1, 0, 1],
                    "B": [1, 0, 1, 1, 1, 0],
                    "C": [1, 1, 0, 1, 1, 1],
                    "D": [1, 1, 1, 0, 1, 1],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "A": [1, 1, 1, 0, 1, 1],
                    "B": [1, 1, 0, 1, 1, 1],
                    "C": [1, 0, 1, 1, 1, 0],
                    "D": [0, 1, 1, 1, 0, 1],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
Given a pandas DataFrame, how does one convert several binary columns (where 1 denotes the value exists, 0 denotes it doesn't) into a single categorical column of lists?

What I would like to accomplish is given a dataframe
df1
   A  B  C  D
0  1  0  1  0
1  0  1  1  0
2  0  0  1  0
3  0  0  0  1
4  1  1  1  1
5  0  1  0  0

could do I convert it into
df1
   A  B  C  D      category
0  1  0  1  0        [A, C]
1  0  1  1  0        [B, C]
2  0  0  1  0           [C]
3  0  0  0  1           [D]
4  1  1  1  1  [A, B, C, D]
5  0  1  0  0           [B]

A:
<code>
import pandas as pd

df = pd.DataFrame({'A': [1, 0, 0, 0, 1, 0],
                   'B': [0, 1, 0, 0, 1, 1],
                   'C': [1, 1, 1, 0, 1, 0],
                   'D': [0, 0, 0, 1, 1, 0]})
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        categories = []
        for i in range(len(df)):
            l = []
            for col in df.columns:
                if df[col].iloc[i] == 1:
                    l.append(col)
            categories.append(l)
        df["category"] = categories
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "A": [1, 0, 0, 0, 1, 0],
                    "B": [0, 1, 0, 0, 1, 1],
                    "C": [1, 1, 1, 0, 1, 0],
                    "D": [0, 0, 0, 1, 1, 0],
                }
            )
        if test_case_id == 2:
            df = pd.DataFrame(
                {
                    "A": [0, 1, 1, 1, 0, 0],
                    "B": [1, 0, 1, 1, 0, 1],
                    "C": [0, 0, 0, 1, 1, 0],
                    "D": [1, 1, 1, 0, 1, 0],
                }
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following DF
        Date
0    2018-01-01
1    2018-02-08
2    2018-02-08
3    2018-02-08
4    2018-02-08

I want to extract the month name and year in a simple way in the following format:
        Date
0    Jan-2018
1    Feb-2018
2    Feb-2018
3    Feb-2018
4    Feb-2018

I have used the df.Date.dt.to_period("M") which returns "2018-01" format.

A:
<code>
import pandas as pd

df = pd.DataFrame({'Date':['2019-01-01','2019-02-08','2019-02-08', '2019-03-08']})
df['Date'] = pd.to_datetime(df['Date'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["Date"] = df["Date"].dt.strftime("%b-%Y")
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {"Date": ["2019-01-01", "2019-02-08", "2019-02-08", "2019-03-08"]}
            )
            df["Date"] = pd.to_datetime(df["Date"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following DF
        Date
0    2018-01-01
1    2018-02-08
2    2018-02-08
3    2018-02-08
4    2018-02-08

I want to extract the month name and year and day in a simple way in the following format:
          Date
0  01-Jan-2018
1  08-Feb-2018
2  08-Feb-2018
3  08-Feb-2018
4  08-Feb-2018

I have used the df.Date.dt.to_period("M") which returns "2018-01" format.

A:
<code>
import pandas as pd

df = pd.DataFrame({'Date':['2019-01-01','2019-02-08','2019-02-08', '2019-03-08']})
df['Date'] = pd.to_datetime(df['Date'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["Date"] = df["Date"].dt.strftime("%d-%b-%Y")
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {"Date": ["2019-01-01", "2019-02-08", "2019-02-08", "2019-03-08"]}
            )
            df["Date"] = pd.to_datetime(df["Date"])
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following DF
	Date
0    2018-01-01
1    2018-02-08
2    2018-02-08
3    2018-02-08
4    2018-02-08

I have another list of two date:
[2017-08-17, 2018-01-31]

For data between 2017-08-17 to 2018-01-31,I want to extract the month name and year and day in a simple way in the following format:

                  Date
0  01-Jan-2018 Tuesday

I have used the df.Date.dt.to_period("M") which returns "2018-01" format.

A:
<code>
import pandas as pd

df = pd.DataFrame({'Date':['2019-01-01','2019-02-08','2019-02-08', '2019-03-08']})
df['Date'] = pd.to_datetime(df['Date'])
List = ['2019-01-17', '2019-02-20']
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

df = df[df['Date'] >= List[0]]
df = df[df['Date'] <= List[1]]
df['Date'] = df['Date'].dt.strftime('%d-%b-%Y %A')

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        data = data
        df, List = data
        df = df[df["Date"] >= List[0]]
        df = df[df["Date"] <= List[1]]
        df["Date"] = df["Date"].dt.strftime("%d-%b-%Y %A")
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {"Date": ["2019-01-01", "2019-02-08", "2019-02-08", "2019-03-08"]}
            )
            df["Date"] = pd.to_datetime(df["Date"])
            List = ["2019-01-17", "2019-02-20"]
        return df, List

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df,List = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
So I have a dataframe that looks like this:
                         #1                     #2
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0

What I want to do is to shift the first row of the first column (11.6985) down 1 row, and then the last row of the first column (72.4399) would be shifted to the first row, first column, like so:
                         #1                     #2
1980-01-01               72.4399                126.0
1980-01-02               11.6985                134.0
1980-01-03               43.6431                130.0
1980-01-04               54.9089                126.0
1980-01-05               63.1225                120.0

The idea is that I want to use these dataframes to find an R^2 value for every shift, so I need to use all the data or it might not work. I have tried to use pandas.Dataframe.shift():
print(data)
#Output
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0
print(data.shift(1,axis = 0))
1980-01-01                   NaN                  NaN
1980-01-02               11.6985                126.0
1980-01-03               43.6431                134.0
1980-01-04               54.9089                130.0
1980-01-05               63.1225                126.0

So it just shifts both columns down and gets rid of the last row of data, which is not what I want.
Any advice?

A:

import pandas as pd

df = pd.DataFrame({'#1': [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                   '#2': [126.0, 134.0, 130.0, 126.0, 120.0]},
                  index=['1980-01-01', '1980-01-02', '1980-01-03', '1980-01-04', '1980-01-05'])

df = ... # put solution in this variable
BEGIN SOLUTION

import numpy as np
df['#1'] = np.roll(df['#1'], shift=1)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["#1"] = np.roll(df["#1"], shift=1)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "#1": [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                    "#2": [126.0, 134.0, 130.0, 126.0, 120.0],
                },
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        elif test_case_id == 2:
            df = pd.DataFrame(
                {"#1": [45, 51, 14, 11, 14], "#2": [126.0, 134.0, 130.0, 126.0, 120.0]},
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
So I have a dataframe that looks like this:
                         #1                     #2
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0

What I want to do is to shift the last row of the first column (72.4399) up 1 row, and then the first row of the first column (11.6985) would be shifted to the last row, first column, like so:
                 #1     #2
1980-01-01  43.6431  126.0
1980-01-02  54.9089  134.0
1980-01-03  63.1225  130.0
1980-01-04  72.4399  126.0
1980-01-05  11.6985  120.0

The idea is that I want to use these dataframes to find an R^2 value for every shift, so I need to use all the data or it might not work. I have tried to use pandas.Dataframe.shift():
print(data)
#Output
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0
print(data.shift(1,axis = 0))
1980-01-01                   NaN                  NaN
1980-01-02               11.6985                126.0
1980-01-03               43.6431                134.0
1980-01-04               54.9089                130.0
1980-01-05               63.1225                126.0

So it just shifts both columns down and gets rid of the last row of data, which is not what I want.
Any advice?

A:

import pandas as pd

df = pd.DataFrame({'#1': [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                   '#2': [126.0, 134.0, 130.0, 126.0, 120.0]},
                  index=['1980-01-01', '1980-01-02', '1980-01-03', '1980-01-04', '1980-01-05'])

df = ... # put solution in this variable
BEGIN SOLUTION

import numpy as np
df['#1'] = np.roll(df['#1'], shift=-1)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["#1"] = np.roll(df["#1"], shift=-1)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "#1": [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                    "#2": [126.0, 134.0, 130.0, 126.0, 120.0],
                },
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        elif test_case_id == 2:
            df = pd.DataFrame(
                {"#1": [45, 51, 14, 11, 14], "#2": [126.0, 134.0, 130.0, 126.0, 120.0]},
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
So I have a dataframe that looks like this:
                         #1                     #2
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0

What I want to do is to shift the first row of the first column (11.6985) down 1 row, and then the last row of the first column (72.4399) would be shifted to the first row, first column.
Then shift the last row of the second column up 1 row, and then the first row of the second column would be shifted to the last row, first column, like so:
                 #1     #2
1980-01-01  72.4399  134.0
1980-01-02  11.6985  130.0
1980-01-03  43.6431  126.0
1980-01-04  54.9089  120.0
1980-01-05  63.1225  126.0

The idea is that I want to use these dataframes to find an R^2 value for every shift, so I need to use all the data or it might not work. I have tried to use pandas.Dataframe.shift():
print(data)
#Output
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0
print(data.shift(1,axis = 0))
1980-01-01                   NaN                  NaN
1980-01-02               11.6985                126.0
1980-01-03               43.6431                134.0
1980-01-04               54.9089                130.0
1980-01-05               63.1225                126.0

So it just shifts both columns down and gets rid of the last row of data, which is not what I want.
Any advice?

A:

import pandas as pd

df = pd.DataFrame({'#1': [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                   '#2': [126.0, 134.0, 130.0, 126.0, 120.0]},
                  index=['1980-01-01', '1980-01-02', '1980-01-03', '1980-01-04', '1980-01-05'])

df = ... # put solution in this variable
BEGIN SOLUTION

import numpy as np
df['#1'] = np.roll(df['#1'], shift=1)
df['#2'] = np.roll(df['#2'], shift=-1)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        df["#1"] = np.roll(df["#1"], shift=1)
        df["#2"] = np.roll(df["#2"], shift=-1)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "#1": [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                    "#2": [126.0, 134.0, 130.0, 126.0, 120.0],
                },
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        elif test_case_id == 2:
            df = pd.DataFrame(
                {"#1": [45, 51, 14, 11, 14], "#2": [126.0, 134.0, 130.0, 126.0, 120.0]},
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
So I have a dataframe that looks like this:
                         #1                     #2
1980-01-01               11.6985                126.0
1980-01-02               43.6431                134.0
1980-01-03               54.9089                130.0
1980-01-04               63.1225                126.0
1980-01-05               72.4399                120.0

What I want to do is to shift the first row of the first column (11.6985) down 1 row, and then the last row of the first column (72.4399) would be shifted to the first row, first column, like so:
                         #1                     #2
1980-01-01               72.4399                126.0
1980-01-02               11.6985                134.0
1980-01-03               43.6431                130.0
1980-01-04               54.9089                126.0
1980-01-05               63.1225                120.0

I want to know how many times after doing this, I can get a Dataframe that minimizes the R^2 values of the first and second columns. I need to output this dataframe:
                 #1     #2
1980-01-01  43.6431  126.0
1980-01-02  54.9089  134.0
1980-01-03  63.1225  130.0
1980-01-04  72.4399  126.0
1980-01-05  11.6985  120.0

Any advice?

A:
<code>
import pandas as pd

df = pd.DataFrame({'#1': [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                   '#2': [126.0, 134.0, 130.0, 126.0, 120.0]},
                  index=['1980-01-01', '1980-01-02', '1980-01-03', '1980-01-04', '1980-01-05'])
</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
def g(df):
    sh = 0
    min_R2 = 0
    for i in range(len(df)):
        min_R2 += (df['#1'].iloc[i]-df['#2'].iloc[i])**2
    for i in range(len(df)):
        R2 = 0
        for j in range(len(df)):
            R2 += (df['#1'].iloc[j] - df['#2'].iloc[j]) ** 2
        if min_R2 > R2:
            sh = i
            min_R2 = R2
        df['#1'] = np.roll(df['#1'], shift=1)
    df['#1'] = np.roll(df['#1'], shift=sh)
    return df

df = g(df)

import pandas as pd
import numpy as np
import copy

def generate_test_case(test_case_id):
    def generate_ans(data):
        df = data
        sh = 0
        min_R2 = 0
        for i in range(len(df)):
            min_R2 += (df["#1"].iloc[i] - df["#2"].iloc[i]) ** 2
        for i in range(len(df)):
            R2 = 0
            for j in range(len(df)):
                R2 += (df["#1"].iloc[j] - df["#2"].iloc[j]) ** 2
            if min_R2 > R2:
                sh = i
                min_R2 = R2
            df["#1"] = np.roll(df["#1"], shift=1)
        df["#1"] = np.roll(df["#1"], shift=sh)
        return df

    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                {
                    "#1": [11.6985, 43.6431, 54.9089, 63.1225, 72.4399],
                    "#2": [126.0, 134.0, 130.0, 126.0, 120.0],
                },
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        elif test_case_id == 2:
            df = pd.DataFrame(
                {"#1": [45, 51, 14, 11, 14], "#2": [126.0, 134.0, 130.0, 126.0, 120.0]},
                index=[
                    "1980-01-01",
                    "1980-01-02",
                    "1980-01-03",
                    "1980-01-04",
                    "1980-01-05",
                ],
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    try:
        pd.testing.assert_frame_equal(result, ans, check_dtype=False)
        return 1
    except:
        return 0

exec_context = r"""
import pandas as pd
import numpy as np
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)