prompt stringlengths 105 4.73k | reference_code stringlengths 11 774 | metadata dict | code_context stringlengths 746 120k |
|---|---|---|---|
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 t... | def g(df, List):
return df.iloc[List]
result = g(df.copy(), List)
| {
"problem_id": 0,
"library_problem_id": 0,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 0
} | 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(
{
... |
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 t... | def g(df, List):
df2 = df.iloc[List].reindex().reset_index(drop=True)
return (df2.Type != df.Type).sum()
result = g(df.copy(), List)
| {
"problem_id": 1,
"library_problem_id": 1,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 0
} | 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 ... |
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', '... | def g(df):
return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")
result = g(df.copy())
| {
"problem_id": 2,
"library_problem_id": 2,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 2
} | 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.DataFram... |
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', 'b... | def g(df):
return df.where(df.apply(lambda x: x.map(x.value_counts())) >= 3, "other")
result = g(df.copy())
| {
"problem_id": 3,
"library_problem_id": 3,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 2
} | 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.DataFram... |
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', '... | result = df.where(df.apply(lambda x: x.map(x.value_counts())) >= 2, "other")
return result
| {
"problem_id": 4,
"library_problem_id": 4,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Surface",
"perturbation_origin_id": 2
} | 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.DataFram... |
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', 'b... | 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())
| {
"problem_id": 5,
"library_problem_id": 5,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 2
} | 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")
... |
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', 'b... | 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 =... | {
"problem_id": 6,
"library_problem_id": 6,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 2
} | 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] >... |
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 ... | def g(df):
return df.loc[(df['keep_if_dup'] =='Yes') | ~df['url'].duplicated()]
result = g(df.copy())
| {
"problem_id": 7,
"library_problem_id": 7,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 7
} | 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(
... |
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.... | def g(df):
return df.loc[(df['drop_if_dup'] =='No') | ~df['url'].duplicated()]
result = g(df.copy())
| {
"problem_id": 8,
"library_problem_id": 8,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 7
} | 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(
... |
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.... | def g(df):
return df.loc[(df['keep_if_dup'] =='Yes') | ~df['url'].duplicated(keep='last')]
result = g(df.copy())
| {
"problem_id": 9,
"library_problem_id": 9,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 7
} | 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.Da... |
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 ... | 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())
| {
"problem_id": 10,
"library_problem_id": 10,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 10
} | 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.colu... |
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 t... | df['datetime'] = df['datetime'].dt.tz_localize(None)
| {
"problem_id": 11,
"library_problem_id": 11,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 11
} | 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:
... |
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 t... | df['datetime'] = df['datetime'].dt.tz_localize(None)
result = df
return result
| {
"problem_id": 12,
"library_problem_id": 12,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Surface",
"perturbation_origin_id": 11
} | 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:
... |
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 t... | 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') | {
"problem_id": 13,
"library_problem_id": 13,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 11
} | 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.strftim... |
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 t... | def g(df):
df['datetime'] = df['datetime'].dt.tz_localize(None)
df.sort_values(by='datetime', inplace=True)
return df
df = g(df.copy())
| {
"problem_id": 14,
"library_problem_id": 14,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 11
} | 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_... |
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 ... | 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')
... | {
"problem_id": 15,
"library_problem_id": 15,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 15
} | 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").valu... |
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.1... | df.loc[df['product'].isin(products), 'score'] *= 10
| {
"problem_id": 16,
"library_problem_id": 16,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 16
} | 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
df.loc[df["product"].isin(prod_list), "score"] *= 10
return df
def define_test_input(test_case_id):
if test_case_id == 1:
... |
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.1... | df.loc[~df['product'].isin(products), 'score'] *= 10
| {
"problem_id": 17,
"library_problem_id": 17,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 16
} | 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
df.loc[~df["product"].isin(prod_list), "score"] *= 10
return df
def define_test_input(test_case_id):
if test_case_id == 1:
... |
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.1... | for product in products:
df.loc[(df['product'] >= product[0]) & (df['product'] <= product[1]), 'score'] *= 10
| {
"problem_id": 18,
"library_problem_id": 18,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 16
} | 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
for product in prod_list:
df.loc[
(df["product"] >= product[0]) & (df["product"] <= product[1]), "score"
... |
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.1... | 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)
| {
"problem_id": 19,
"library_problem_id": 19,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 16
} | 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[... |
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 bi... | df["category"] = df.idxmax(axis=1)
| {
"problem_id": 20,
"library_problem_id": 20,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 20
} | 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(
{
... |
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... | df["category"] = df.idxmin(axis=1)
| {
"problem_id": 21,
"library_problem_id": 21,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 20
} | 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(
{
... |
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 ... | 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
| {
"problem_id": 22,
"library_problem_id": 22,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 20
} | 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(co... |
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.... | df['Date'] = df['Date'].dt.strftime('%b-%Y')
| {
"problem_id": 23,
"library_problem_id": 23,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 23
} | 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(
... |
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 use... | df['Date'] = df['Date'].dt.strftime('%d-%b-%Y')
| {
"problem_id": 24,
"library_problem_id": 24,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 23
} | 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(
... |
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:
... | df = df[df['Date'] >= List[0]]
df = df[df['Date'] <= List[1]]
df['Date'] = df['Date'].dt.strftime('%d-%b-%Y %A') | {
"problem_id": 25,
"library_problem_id": 25,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 23
} | 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
... |
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
... | import numpy as np
df['#1'] = np.roll(df['#1'], shift=1) | {
"problem_id": 26,
"library_problem_id": 26,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 26
} | 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(
... |
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
... | import numpy as np
df['#1'] = np.roll(df['#1'], shift=-1) | {
"problem_id": 27,
"library_problem_id": 27,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 26
} | 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(
... |
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
... | import numpy as np
df['#1'] = np.roll(df['#1'], shift=1)
df['#2'] = np.roll(df['#2'], shift=-1) | {
"problem_id": 28,
"library_problem_id": 28,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 26
} | 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:
... |
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
... | 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 =... | {
"problem_id": 29,
"library_problem_id": 29,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 26
} | 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... |
Problem:
Considering a simple df:
HeaderA | HeaderB | HeaderC
476 4365 457
Is there a way to rename all columns, for example to add to all columns an "X" in the end?
HeaderAX | HeaderBX | HeaderCX
476 4365 457
I am concatenating multiple dataframes and want to easily differentiate the... | def g(df):
return df.add_suffix('X')
df = g(df.copy())
| {
"problem_id": 30,
"library_problem_id": 30,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 30
} | 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.add_suffix("X")
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"HeaderA": [476], "HeaderB": [4365], "HeaderC"... |
Problem:
Considering a simple df:
HeaderA | HeaderB | HeaderC
476 4365 457
Is there a way to rename all columns, for example to add to all columns an "X" in the head?
XHeaderA | XHeaderB | XHeaderC
476 4365 457
I am concatenating multiple dataframes and want to easily differentiate the... | def g(df):
return df.add_prefix('X')
df = g(df.copy())
| {
"problem_id": 31,
"library_problem_id": 31,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 30
} | 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.add_prefix("X")
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"HeaderA": [476], "HeaderB": [4365], "HeaderC"... |
Problem:
Considering a simple df:
HeaderA | HeaderB | HeaderC | HeaderX
476 4365 457 345
Is there a way to rename all columns, for example to add to columns which don’t end with "X" and add to all columns an "X" in the head?
XHeaderAX | XHeaderBX | XHeaderCX | XHeaderX
476 4365 457... | def g(df):
for col in df.columns:
if not col.endswith('X'):
df.rename(columns={col: col+'X'}, inplace=True)
return df.add_prefix('X')
df = g(df.copy())
| {
"problem_id": 32,
"library_problem_id": 32,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 30
} | 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:
if not col.endswith("X"):
df.rename(columns={col: col + "X"}, inplace=True)
return df.add_prefix("X")
def defin... |
Problem:
I have a script that generates a pandas data frame with a varying number of value columns. As an example, this df might be
import pandas as pd
df = pd.DataFrame({
'group': ['A', 'A', 'A', 'B', 'B'],
'group_color' : ['green', 'green', 'green', 'blue', 'blue'],
'val1': [5, 2, 3, 4, 5],
'val2' : [4, 2, 8, 5, 7]
... | def g(df):
return df.groupby('group').agg(lambda x : x.head(1) if x.dtype=='object' else x.mean())
result = g(df.copy())
| {
"problem_id": 33,
"library_problem_id": 33,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 33
} | 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.groupby("group").agg(
lambda x: x.head(1) if x.dtype == "object" else x.mean()
)
def define_test_input(test_case_id):
if test_case_i... |
Problem:
I have a script that generates a pandas data frame with a varying number of value columns. As an example, this df might be
import pandas as pd
df = pd.DataFrame({
'group': ['A', 'A', 'A', 'B', 'B'],
'group_color' : ['green', 'green', 'green', 'blue', 'blue'],
'val1': [5, 2, 3, 4, 5],
'val2' : [4, 2, 8, 5, 7]
... | def g(df):
return df.groupby('group').agg(lambda x : x.head(1) if x.dtype=='object' else x.sum())
result = g(df.copy())
| {
"problem_id": 34,
"library_problem_id": 34,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 33
} | 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.groupby("group").agg(
lambda x: x.head(1) if x.dtype == "object" else x.sum()
)
def define_test_input(test_case_id):
if test_case_id... |
Problem:
I have a script that generates a pandas data frame with a varying number of value columns. As an example, this df might be
import pandas as pd
df = pd.DataFrame({
'group': ['A', 'A', 'A', 'B', 'B'],
'group_color' : ['green', 'green', 'green', 'blue', 'blue'],
'val1': [5, 2, 3, 4, 5],
'val2' : [4, 2, 8, 5, 7]
... | def g(df):
return df.groupby('group').agg(lambda x : x.head(1) if x.dtype=='object' else x.mean() if x.name.endswith('2') else x.sum())
result = g(df.copy())
| {
"problem_id": 35,
"library_problem_id": 35,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 33
} | 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.groupby("group").agg(
lambda x: (
x.head(1)
if x.dtype == "object"
else x.mean() if x.name.endswith("2") ... |
Problem:
I have pandas df with say, 100 rows, 10 columns, (actual data is huge). I also have row_index list which contains, which rows to be considered to take mean. I want to calculate mean on say columns 2,5,6,7 and 8. Can we do it with some function for dataframe object?
What I know is do a for loop, get value of ro... | def g(df, row_list, column_list):
return df[column_list].iloc[row_list].mean(axis=0)
result = g(df.copy(),row_list,column_list)
| {
"problem_id": 36,
"library_problem_id": 36,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 36
} | import pandas as pd
import numpy as np
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, row_list, column_list = data
return df[column_list].iloc[row_list].mean(axis=0)
def define_test_input(test_case_id):
if test_cas... |
Problem:
I have pandas df with say, 100 rows, 10 columns, (actual data is huge). I also have row_index list which contains, which rows to be considered to take sum. I want to calculate sum on say columns 2,5,6,7 and 8. Can we do it with some function for dataframe object?
What I know is do a for loop, get value of row ... | def g(df, row_list, column_list):
return df[column_list].iloc[row_list].sum(axis=0)
result = g(df.copy(), row_list, column_list)
| {
"problem_id": 37,
"library_problem_id": 37,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 36
} | import pandas as pd
import numpy as np
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, row_list, column_list = data
return df[column_list].iloc[row_list].sum(axis=0)
def define_test_input(test_case_id):
if test_case... |
Problem:
I have pandas df with say, 100 rows, 10 columns, (actual data is huge). I also have row_index list which contains, which rows to be considered to take sum. I want to calculate sum on say columns 2,5,6,7 and 8. Can we do it with some function for dataframe object?
What I know is do a for loop, get value of row ... | def g(df, row_list, column_list):
result = df[column_list].iloc[row_list].sum(axis=0)
return result.drop(result.index[result.argmax()])
result = g(df.copy(), row_list, column_list)
| {
"problem_id": 38,
"library_problem_id": 38,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 36
} | import pandas as pd
import numpy as np
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, row_list, column_list = data
result = df[column_list].iloc[row_list].sum(axis=0)
return result.drop(result.index[result.argmax()])
... |
Problem:
I have a dataframe with numerous columns (≈30) from an external source (csv file) but several of them have no value or always the same. Thus, I would to see quickly the value_counts for each column. How can i do that?
For example
id, temp, name
1 34, null, mark
2 22, null, mark
3 34, null, mark
Please retu... | def g(df):
return df.apply(lambda x: x.value_counts()).T.stack()
result = g(df.copy())
| {
"problem_id": 39,
"library_problem_id": 39,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 39
} | 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.apply(lambda x: x.value_counts()).T.stack()
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame(
da... |
Problem:
I have a dataframe with numerous columns (≈30) from an external source (csv file) but several of them have no value or always the same. Thus, I would to see quickly the counts of 'null' for each column. How can i do that?
For example
id, temp, name
1 34, null, null
2 22, null, mark
3 34, null, mark
Please ... | def g(df):
return df.apply(lambda x: x.value_counts()).T.null
result = g(df.copy())
| {
"problem_id": 40,
"library_problem_id": 40,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 39
} | 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.apply(lambda x: x.value_counts()).T.null
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame(
data=... |
Problem:
I have a dataframe with numerous columns (≈30) from an external source (csv file) but several of them have no value or always the same. Thus, I would to see quickly the value_counts for each column. How can i do that?
For example
id, temp, name
1 34, null, mark
2 22, null, mark
3 34, null, mark
Please retur... | def g(df):
s = ''
for c in df.columns:
s += "---- %s ---" % c
s += "\n"
s += str(df[c].value_counts())
s += "\n"
return s
result = g(df.copy())
| {
"problem_id": 41,
"library_problem_id": 41,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 39
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
s = ""
for c in df.columns:
s += "---- %s ---" % c
s += "\n"
s += str(df[c].value_counts())
s += "\n"
return s
... |
Problem:
I am trying to clean up a Excel file for some further research. Problem that I have, I want to merge the first and second row. The code which I have now:
xl = pd.ExcelFile("nanonose.xls")
df = xl.parse("Sheet1")
df = df.drop('Unnamed: 2', axis=1)
## Tried this line but no luck
##print(df.head().combine_first(... | def g(df):
df.columns = np.concatenate([df.iloc[0, :2], df.columns[2:]])
df = df.iloc[1:].reset_index(drop=True)
return df
result = g(df.copy())
| {
"problem_id": 42,
"library_problem_id": 42,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 42
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.columns = np.concatenate([df.iloc[0, :2], df.columns[2:]])
df = df.iloc[1:].reset_index(drop=True)
return df
def define_test_input(test_case_id):
... |
Problem:
I am trying to clean up a Excel file for some further research. Problem that I have, I want to merge the first and second row. The code which I have now:
xl = pd.ExcelFile("nanonose.xls")
df = xl.parse("Sheet1")
df = df.drop('Unnamed: 2', axis=1)
## Tried this line but no luck
##print(df.head().combine_first(... | def g(df):
df.columns = np.concatenate([df.columns[0:1], df.iloc[0, 1:2], df.columns[2:]])
df = df.iloc[1:].reset_index(drop=True)
return df
result = g(df.copy())
| {
"problem_id": 43,
"library_problem_id": 43,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 42
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.columns = np.concatenate([df.columns[0:1], df.iloc[0, 1:2], df.columns[2:]])
df = df.iloc[1:].reset_index(drop=True)
return df
def define_test_input(te... |
Problem:
I have a DataFrame like :
0 1 2
0 0.0 1.0 2.0
1 NaN 1.0 2.0
2 NaN NaN 2.0
What I want to get is
Out[116]:
0 1 2
0 0.0 1.0 2.0
1 1.0 2.0 NaN
2 2.0 NaN NaN
This is my approach as of now.
df.apply(lambda x : (x[x.notnull()].values.tolist()+x[x.isnull()].values.tolist())... | def justify(a, invalid_val=0, axis=1, side='left'):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
mask = a!=invalid_val
justified_mask = np.sort(mask,axis=axis)
if (side=='up') | (side=='left'):
justified_mask = np.flip(justified_mask,axis=axis)
out = np.full(a.shape, i... | {
"problem_id": 44,
"library_problem_id": 44,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 44
} | 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
def justify(a, invalid_val=0, axis=1, side="left"):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
... |
Problem:
I have a DataFrame like :
0 1 2
0 0.0 1.0 2.0
1 1.0 2.0 NaN
2 2.0 NaN NaN
What I want to get is
Out[116]:
0 1 2
0 0.0 1.0 2.0
1 Nan 1.0 2.0
2 NaN NaN 2.0
This is my approach as of now.
df.apply(lambda x : (x[x.isnull()].values.tolist()+x[x.notnull()].values.tolist())... | def justify(a, invalid_val=0, axis=1, side='left'):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
mask = a!=invalid_val
justified_mask = np.sort(mask,axis=axis)
if (side=='up') | (side=='left'):
justified_mask = np.flip(justified_mask,axis=axis)
out = np.full(a.shape, i... | {
"problem_id": 45,
"library_problem_id": 45,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 44
} | 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
def justify(a, invalid_val=0, axis=1, side="left"):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
... |
Problem:
I have a DataFrame like :
0 1 2
0 0.0 1.0 2.0
1 NaN 1.0 2.0
2 NaN NaN 2.0
What I want to get is
Out[116]:
0 1 2
0 NaN NaN 2.0
1 NaN 1.0 2.0
2 0.0 1.0 2.0
This is my approach as of now.
df.apply(lambda x : (x[x.isnull()].values.tolist()+x[x.notnull()].values.tolist())... | def justify(a, invalid_val=0, axis=1, side='left'):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
mask = a!=invalid_val
justified_mask = np.sort(mask,axis=axis)
if (side=='up') | (side=='left'):
justified_mask = np.flip(justified_mask,axis=axis)
out = np.full(a.shape, i... | {
"problem_id": 46,
"library_problem_id": 46,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 44
} | 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
def justify(a, invalid_val=0, axis=1, side="left"):
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
... |
Problem:
I have a pandas dataframe structured like this:
value
lab
A 50
B 35
C 8
D 5
E 1
F 1
This is just an example, the actual dataframe is bigger, but follows the same structure.
The sample dataframe has been created with this two lines:
df = pd.DataFrame... | def g(df, thresh):
return (df[lambda x: x['value'] >= thresh] .append(df[lambda x: x['value'] < thresh].sum().rename('X')))
result = g(df.copy(),thresh)
| {
"problem_id": 47,
"library_problem_id": 47,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 47
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, thresh = data
return df[lambda x: x["value"] >= thresh].append(
df[lambda x: x["value"] < thresh].sum().rename("X")
)
def define_test_in... |
Problem:
I have a pandas dataframe structured like this:
value
lab
A 50
B 35
C 8
D 5
E 1
F 1
This is just an example, the actual dataframe is bigger, but follows the same structure.
The sample dataframe has been created with this two lines:
df = pd.DataFrame... | def g(df, thresh):
return (df[lambda x: x['value'] <= thresh]
.append(df[lambda x: x['value'] > thresh].mean().rename('X')))
result = g(df.copy(),thresh)
| {
"problem_id": 48,
"library_problem_id": 48,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 47
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, thresh = data
return df[lambda x: x["value"] <= thresh].append(
df[lambda x: x["value"] > thresh].mean().rename("X")
)
def define_test_i... |
Problem:
I have a pandas dataframe structured like this:
value
lab
A 50
B 35
C 8
D 5
E 1
F 1
This is just an example, the actual dataframe is bigger, but follows the same structure.
The sample dataframe has been created with this two lines:
df = pd.DataFrame(... | def g(df, section_left, section_right):
return (df[lambda x: x['value'].between(section_left, section_right)]
.append(df[lambda x: ~x['value'].between(section_left, section_right)].mean().rename('X')))
result = g(df.copy(),section_left, section_right)
| {
"problem_id": 49,
"library_problem_id": 49,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 47
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, section_left, section_right = data
return df[lambda x: x["value"].between(section_left, section_right)].append(
df[lambda x: ~x["value"].between(sect... |
Problem:
Sample dataframe:
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
I'd like to add inverses of each existing column to the dataframe and name them based on existing column names with a prefix, e.g. inv_A is an inverse of column A and so on.
The resulting dataframe should look like so:
result = pd.DataFrame... | def g(df):
return df.join(df.apply(lambda x: 1/x).add_prefix('inv_'))
result = g(df.copy())
| {
"problem_id": 50,
"library_problem_id": 50,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 50
} | 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
return df.join(df.apply(lambda x: 1 / x).add_prefix("inv_"))
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.Da... |
Problem:
Sample dataframe:
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
I'd like to add exponentials of each existing column to the dataframe and name them based on existing column names with a prefix, e.g. exp_A is an exponential of column A and so on.
The resulting dataframe should look like so:
result = pd.D... | import math
def g(df):
return df.join(df.apply(lambda x: math.e**x).add_prefix('exp_'))
result = g(df.copy())
| {
"problem_id": 51,
"library_problem_id": 51,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 50
} | import pandas as pd
import numpy as np
import math
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
return df.join(df.apply(lambda x: math.e**x).add_prefix("exp_"))
def define_test_input(test_case_id):
if test_case_id == 1:
... |
Problem:
Sample dataframe:
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 0]})
I'd like to add inverses of each existing column to the dataframe and name them based on existing column names with a prefix, e.g. inv_A is an inverse of column A and so on.
Notice that 0 has no inverse and please keep it in inv_A
The resul... | import math
def g(df):
return df.join(df.apply(lambda x: 1/x).add_prefix('inv_')).replace(math.inf, 0)
result = g(df.copy())
| {
"problem_id": 52,
"library_problem_id": 52,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 50
} | import pandas as pd
import numpy as np
import math
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
return df.join(df.apply(lambda x: 1 / x).add_prefix("inv_")).replace(
math.inf, 0
)
def define_test_input(test_case_id... |
Problem:
Sample dataframe:
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
I'd like to add sigmoids of each existing column to the dataframe and name them based on existing column names with a prefix, e.g. sigmoid_A is an sigmoid of column A and so on.
The resulting dataframe should look like so:
result = pd.DataF... | import math
def g(df):
return df.join(df.apply(lambda x: 1/(1+math.e**(-x))).add_prefix('sigmoid_'))
result = g(df.copy())
| {
"problem_id": 53,
"library_problem_id": 53,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 50
} | import pandas as pd
import numpy as np
import math
import copy
import tokenize, io
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
return df.join(
df.apply(lambda x: 1 / (1 + math.e ** (-x))).add_prefix("sigmoid_")
)
def define_test_input(test_case_... |
Problem:
The title might not be intuitive--let me provide an example. Say I have df, created with:
a = np.array([[ 1. , 0.9, 1. ],
[ 0.9, 0.9, 1. ],
[ 0.8, 1. , 0.5],
[ 1. , 0.3, 0.2],
[ 1. , 0.2, 0.1],
[ 0.9, 1. , 1. ],
[ ... | def g(df):
return df.mask((df == df.min()).cumsum().astype(bool))[::-1].idxmax()
result = g(df.copy())
| {
"problem_id": 54,
"library_problem_id": 54,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 54
} | 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.mask((df == df.min()).cumsum().astype(bool))[::-1].idxmax()
def define_test_input(test_case_id):
if test_case_id == 1:
a = np.array(
... |
Problem:
The title might not be intuitive--let me provide an example. Say I have df, created with:
a = np.array([[ 1. , 0.9, 1. ],
[ 0.9, 0.9, 1. ],
[ 0.8, 1. , 0.5],
[ 1. , 0.3, 0.2],
[ 1. , 0.2, 0.1],
[ 0.9, 1. , 1. ],
[ ... | def g(df):
return df.mask(~(df == df.min()).cumsum().astype(bool)).idxmax()
result = g(df.copy())
| {
"problem_id": 55,
"library_problem_id": 55,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 54
} | 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.mask(~(df == df.min()).cumsum().astype(bool)).idxmax()
def define_test_input(test_case_id):
if test_case_id == 1:
a = np.array(
... |
Problem:
I've a data frame that looks like the following
x = pd.DataFrame({'user': ['a','a','b','b'], 'dt': ['2016-01-01','2016-01-02', '2016-01-05','2016-01-06'], 'val': [1,33,2,1]})
What I would like to be able to do is find the minimum and maximum date within the date column and expand that column to have all the ... | def g(df):
df.dt = pd.to_datetime(df.dt)
return df.set_index(['dt', 'user']).unstack(fill_value=0).asfreq('D', fill_value=0).stack().sort_index(level=1).reset_index()
result = g(df.copy())
| {
"problem_id": 56,
"library_problem_id": 56,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 56
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.dt = pd.to_datetime(df.dt)
return (
df.set_index(["dt", "user"])
.unstack(fill_value=0)
.asfreq("D", fill_value=0)
.... |
Problem:
I've a data frame that looks like the following
x = pd.DataFrame({'user': ['abc','abc','efg','efg'], 'dt': ['2022-01-01','2022-01-02', '2022-01-05','2022-01-06'], 'val': [1,14,51,4]})
What I would like to be able to do is find the minimum and maximum date within the date column and expand that column to have... | def g(df):
return df.set_index(['dt', 'user']).unstack(fill_value=0).asfreq('D', fill_value=0).stack().sort_index(level=1).reset_index()
result = g(df.copy())
| {
"problem_id": 57,
"library_problem_id": 57,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Surface",
"perturbation_origin_id": 56
} | 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.set_index(["dt", "user"])
.unstack(fill_value=0)
.asfreq("D", fill_value=0)
.stack()
.sort_index(level=... |
Problem:
I've a data frame that looks like the following
x = pd.DataFrame({'user': ['a','a','b','b'], 'dt': ['2016-01-01','2016-01-02', '2016-01-05','2016-01-06'], 'val': [1,33,2,1]})
What I would like to be able to do is find the minimum and maximum date within the date column and expand that column to have all the ... | def g(df):
df.dt = pd.to_datetime(df.dt)
return df.set_index(['dt', 'user']).unstack(fill_value=233).asfreq('D', fill_value=233).stack().sort_index(level=1).reset_index()
result = g(df.copy())
| {
"problem_id": 58,
"library_problem_id": 58,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 56
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.dt = pd.to_datetime(df.dt)
return (
df.set_index(["dt", "user"])
.unstack(fill_value=233)
.asfreq("D", fill_value=233)
... |
Problem:
I've a data frame that looks like the following
x = pd.DataFrame({'user': ['a','a','b','b'], 'dt': ['2016-01-01','2016-01-02', '2016-01-05','2016-01-06'], 'val': [1,33,2,1]})
What I would like to be able to do is find the minimum and maximum date within the date column and expand that column to have all the ... | def g(df):
df.dt = pd.to_datetime(df.dt)
result = df.set_index(['dt', 'user']).unstack(fill_value=-11414).asfreq('D', fill_value=-11414)
for col in result.columns:
Max = result[col].max()
for idx in result.index:
if result.loc[idx, col] == -11414:
result.loc[idx, ... | {
"problem_id": 59,
"library_problem_id": 59,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 56
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.dt = pd.to_datetime(df.dt)
result = (
df.set_index(["dt", "user"])
.unstack(fill_value=-11414)
.asfreq("D", fill_value=-11414)
... |
Problem:
I've a data frame that looks like the following
x = pd.DataFrame({'user': ['a','a','b','b'], 'dt': ['2016-01-01','2016-01-02', '2016-01-05','2016-01-06'], 'val': [1,33,2,1]})
What I would like to be able to do is find the minimum and maximum date within the date column and expand that column to have all the ... | def g(df):
df.dt = pd.to_datetime(df.dt)
result = df.set_index(['dt', 'user']).unstack(fill_value=-11414).asfreq('D', fill_value=-11414)
for col in result.columns:
Max = result[col].max()
for idx in result.index:
if result.loc[idx, col] == -11414:
result.loc[idx, ... | {
"problem_id": 60,
"library_problem_id": 60,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 56
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.dt = pd.to_datetime(df.dt)
result = (
df.set_index(["dt", "user"])
.unstack(fill_value=-11414)
.asfreq("D", fill_value=-11414)
... |
Problem:
I am using Pandas to get a dataframe like this:
name a b c
0 Aaron 3 5 7
1 Aaron 3 6 9
2 Aaron 3 6 10
3 Brave 4 6 0
4 Brave 3 6 1
I want to replace each name with a unique ID so output looks like:
name a b c
0 1 3 5 7
1 1 3 6 9
2 1 3 6 10
3 2 4 6... | def g(df):
F = {}
cnt = 0
for i in range(len(df)):
if df['name'].iloc[i] not in F.keys():
cnt += 1
F[df['name'].iloc[i]] = cnt
df.loc[i,'name'] = F[df.loc[i,'name']]
return df
result = g(df.copy())
| {
"problem_id": 61,
"library_problem_id": 61,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 61
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
F = {}
cnt = 0
for i in range(len(df)):
if df["name"].iloc[i] not in F.keys():
cnt += 1
F[df["name"].iloc[i]] = cnt... |
Problem:
I am using Pandas to get a dataframe like this:
name a b c
0 Aaron 3 5 7
1 Aaron 3 6 9
2 Aaron 3 6 10
3 Brave 4 6 0
4 Brave 3 6 1
5 David 5 1 4
I want to replace each a with a unique ID so output looks like:
name a b c
0 Aaron 1 5 7
1 Aaron 1 6 9
2 Aaron... | def g(df):
F = {}
cnt = 0
for i in range(len(df)):
if df['a'].iloc[i] not in F.keys():
cnt += 1
F[df['a'].iloc[i]] = cnt
df.loc[i, 'a'] = F[df.loc[i, 'a']]
return df
result = g(df.copy())
| {
"problem_id": 62,
"library_problem_id": 62,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 61
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
F = {}
cnt = 0
for i in range(len(df)):
if df["a"].iloc[i] not in F.keys():
cnt += 1
F[df["a"].iloc[i]] = cnt
... |
Problem:
I am using Pandas to get a dataframe like this:
name a b c
0 Aaron 3 5 7
1 Aaron 3 6 9
2 Aaron 3 6 10
3 Brave 4 6 0
4 Brave 3 6 1
I want to replace each name with a unique ID so output looks like:
name a b c
0 1 3 5 7
1 1 3 6 9
2 1 3 6 10
3 2 4 6... | F = {}
cnt = 0
for i in range(len(df)):
if df['name'].iloc[i] not in F.keys():
cnt += 1
F[df['name'].iloc[i]] = cnt
df.loc[i,'name'] = F[df.loc[i,'name']]
result = df
return result
| {
"problem_id": 63,
"library_problem_id": 63,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Surface",
"perturbation_origin_id": 61
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
F = {}
cnt = 0
for i in range(len(df)):
if df["name"].iloc[i] not in F.keys():
cnt += 1
F[df["name"].iloc[i]] = cnt... |
Problem:
I am using Pandas to get a dataframe like this:
name a b c
0 Aaron 3 5 7
1 Aaron 3 6 9
2 Aaron 3 6 10
3 Brave 4 6 0
4 Brave 3 6 1
I want to combine name and a and replace each of them with a unique ID so output looks like:
ID b c
0 1 5 7
1 1 6 9
2 1 6 10
... | def g(df):
df['ID'] = df["name"].map(str) +"-"+ df["a"].map(str)
cnt = 0
F = {}
for i in range(len(df)):
if df['ID'].iloc[i] not in F.keys():
cnt += 1
F[df['ID'].iloc[i]] = cnt
df.loc[i,'ID'] = F[df.loc[i,'ID']]
del df['name']
del df['a']
df = df[['ID'... | {
"problem_id": 64,
"library_problem_id": 64,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 61
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df["ID"] = df["name"].map(str) + "-" + df["a"].map(str)
cnt = 0
F = {}
for i in range(len(df)):
if df["ID"].iloc[i] not in F.keys():
... |
Problem:
I have a table like this.
user 01/12/15 02/12/15 someBool
u1 100 300 True
u2 200 -100 False
u3 -50 200 True
I want to repartition the date columns into two columns date and value like this.
user date value someBool
u1 01/12/15 100 ... | def g(df):
df = df.set_index(['user','someBool']).stack().reset_index(name='value').rename(columns={'level_2':'date'})
return df[['user', 'date', 'value', 'someBool']]
df = g(df.copy()) | {
"problem_id": 65,
"library_problem_id": 65,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 65
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df = (
df.set_index(["user", "someBool"])
.stack()
.reset_index(name="value")
.rename(columns={"level_2": "date"})
)
... |
Problem:
I have a table like this.
user 01/12/15 02/12/15 someBool
u1 100 300 True
u2 200 -100 False
u3 -50 200 True
I want to repartition the others columns into two columns others and value like this.
user 01/12/15 others value
0 u1 100 02/1... | def g(df):
return df.set_index(['user','01/12/15']).stack().reset_index(name='value').rename(columns={'level_2':'others'})
df = g(df.copy())
| {
"problem_id": 66,
"library_problem_id": 66,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 65
} | 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.set_index(["user", "01/12/15"])
.stack()
.reset_index(name="value")
.rename(columns={"level_2": "others"})
... |
Problem:
I have a table like this.
user 01/12/15 02/12/15 someBool
u1 100 None True
u2 200 -100 False
u3 None 200 True
I want to repartition the date columns into two columns date and value like this.
user date value someBool
u1 01/12/15 100... | def g(df):
df = df.set_index(['user','someBool']).stack().reset_index(name='value').rename(columns={'level_2':'date'})
return df[['user', 'date', 'value', 'someBool']]
df = g(df.copy())
| {
"problem_id": 67,
"library_problem_id": 67,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 65
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df = (
df.set_index(["user", "someBool"])
.stack()
.reset_index(name="value")
.rename(columns={"level_2": "date"})
)
... |
Problem:
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
print df
a b c d e
0 0.945686 0.000710 0.... | def g(df, columns):
return df.loc[df['c']>0.5,columns]
result = g(df.copy(), columns)
| {
"problem_id": 68,
"library_problem_id": 68,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 68
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, columns = data
return df.loc[df["c"] > 0.5, columns]
def define_test_input(test_case_id):
if test_case_id == 1:
np.random.seed(2)
... |
Problem:
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
print df
a b c d e
0 0.945686 0.000710 0.... | result = df.loc[df['c']>0.45,columns]
| {
"problem_id": 69,
"library_problem_id": 69,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 68
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, columns = data
return df.loc[df["c"] > 0.45, columns]
def define_test_input(test_case_id):
if test_case_id == 1:
np.random.seed(2)
... |
Problem:
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
print df
a b c d e
0 0.945686 0.000710 0.... | result = df.loc[df['c']>0.5,columns].to_numpy()
return result
| {
"problem_id": 70,
"library_problem_id": 70,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 68
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, columns = data
return df.loc[df["c"] > 0.5, columns].to_numpy()
def define_test_input(test_case_id):
if test_case_id == 1:
np.random.see... |
Problem:
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame, then compute and append sum of the two columns for each element to the right of original columns.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = lis... | ans = df[df.c > 0.5][columns]
ans['sum'] = ans.sum(axis=1)
result = ans
return result
| {
"problem_id": 71,
"library_problem_id": 71,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 68
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, columns = data
ans = df[df.c > 0.5][columns]
ans["sum"] = ans.sum(axis=1)
return ans
def define_test_input(test_case_id):
if test_ca... |
Problem:
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
print df
a b c d e
0 0.945686 0.000710 0.... | result = df.loc[df['c']>0.5,columns]
return result
| {
"problem_id": 72,
"library_problem_id": 72,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 68
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, columns = data
return df.loc[df["c"] > 0.5, columns]
def define_test_input(test_case_id):
if test_case_id == 1:
np.random.seed(42)
... |
Problem:
I have a pandas dataframe that looks like the following:
ID date close
1 09/15/07 123.45
2 06/01/08 130.13
3 10/25/08 132.01
4 05/13/09 118.34
5 11/07/09 145.99
6 11/15/09 146.73
7 07/03/11 171.10
I want to remove any rows that overlap.
Overlapping rows is defined as any ... | def g(df, X):
t = df['date']
df['date'] = pd.to_datetime(df['date'])
filter_ids = [0]
last_day = df.loc[0, "date"]
for index, row in df[1:].iterrows():
if (row["date"] - last_day).days > X:
filter_ids.append(index)
last_day = row["date"]
df['date'] = t
return ... | {
"problem_id": 73,
"library_problem_id": 73,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 73
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, X = data
t = df["date"]
df["date"] = pd.to_datetime(df["date"])
filter_ids = [0]
last_day = df.loc[0, "date"]
for index, row in d... |
Problem:
I have a pandas dataframe that looks like the following:
ID date close
1 09/15/07 123.45
2 06/01/08 130.13
3 10/25/08 132.01
4 05/13/09 118.34
5 11/07/09 145.99
6 11/15/09 146.73
7 07/03/11 171.10
I want to remove any rows that overlap.
Overlapping rows is defined as any ... | def g(df, X):
t = df['date']
df['date'] = pd.to_datetime(df['date'])
X *= 7
filter_ids = [0]
last_day = df.loc[0, "date"]
for index, row in df[1:].iterrows():
if (row["date"] - last_day).days > X:
filter_ids.append(index)
last_day = row["date"]
df['date'] = t
... | {
"problem_id": 74,
"library_problem_id": 74,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 73
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, X = data
t = df["date"]
df["date"] = pd.to_datetime(df["date"])
X *= 7
filter_ids = [0]
last_day = df.loc[0, "date"]
for ... |
Problem:
I have a pandas dataframe that looks like the following:
ID date close
1 09/15/07 123.45
2 06/01/08 130.13
3 10/25/08 132.01
4 05/13/09 118.34
5 11/07/09 145.99
6 11/15/09 146.73
7 07/03/11 171.10
I want to remove any rows that overlapand convert df to the following format:... | def g(df, X):
df['date'] = pd.to_datetime(df['date'])
X *= 7
filter_ids = [0]
last_day = df.loc[0, "date"]
for index, row in df[1:].iterrows():
if (row["date"] - last_day).days > X:
filter_ids.append(index)
last_day = row["date"]
df['date'] = df['date'].dt.strftim... | {
"problem_id": 75,
"library_problem_id": 75,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 73
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df, X = data
df["date"] = pd.to_datetime(df["date"])
X *= 7
filter_ids = [0]
last_day = df.loc[0, "date"]
for index, row in df[1:].it... |
Problem:
I have a simple dataframe which I would like to bin for every 3 rows.
It looks like this:
col1
0 2
1 1
2 3
3 1
4 0
and I would like to turn it into this:
col1
0 2
1 0.5
I have already posted a similar question here but I have no Idea how to port the solution to my... | def g(df):
return df.groupby(df.index // 3).mean()
result = g(df.copy())
| {
"problem_id": 76,
"library_problem_id": 76,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 76
} | 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.groupby(df.index // 3).mean()
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"col1": [2, 1, 3, 1, 0]})
... |
Problem:
I have a simple dataframe which I would like to bin for every 3 rows.
It looks like this:
col1
0 1
1 1
2 4
3 5
4 1
and I would like to turn it into this:
col1
0 2
1 3
I have already posted a similar question here but I have no Idea how to port the solution to my... | def g(df):
return df.groupby(df.index // 3).mean()
result = g(df.copy())
| {
"problem_id": 77,
"library_problem_id": 77,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Surface",
"perturbation_origin_id": 76
} | 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.groupby(df.index // 3).mean()
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"col1": [1, 1, 4, 5, 1]})
... |
Problem:
I have a simple dataframe which I would like to bin for every 4 rows.
It looks like this:
col1
0 1
1 1
2 4
3 5
4 1
5 4
and I would like to turn it into this:
col1
0 11
1 5
I have already posted a similar question here but I have no Idea how to port the solut... | def g(df):
return df.groupby(df.index // 4).sum()
result = g(df.copy())
| {
"problem_id": 78,
"library_problem_id": 78,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 76
} | 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.groupby(df.index // 4).sum()
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"col1": [1, 1, 4, 5, 1, 4]})
... |
Problem:
I have a simple dataframe which I would like to bin for every 3 rows from back to front.
It looks like this:
col1
0 2
1 1
2 3
3 1
4 0
and I would like to turn it into this:
col1
0 1.5
1 1.333
I have already posted a similar question here but I have no Idea how to po... | def g(df):
return df.groupby((df.index+(-df.size % 3)) // 3).mean()
result = g(df.copy())
| {
"problem_id": 79,
"library_problem_id": 79,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 76
} | 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.groupby((df.index + (-df.size % 3)) // 3).mean()
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.DataFrame({"col1": [2, 1... |
Problem:
I have a simple dataframe which I would like to bin for every 3 rows to get sum and 2 rows to get avg.That means for the first 3 rows get their sum, then 2 rows get their avg, then 3 rows get their sum, then 2 rows get their avg…
It looks like this:
col1
0 2
1 1
2 3
3 1
4 0
5 ... | def g(df):
l = []
for i in range(2*(len(df) // 5) + (len(df) % 5) // 3 + 1):
l.append(0)
for i in range(len(df)):
idx = 2*(i // 5) + (i % 5) // 3
if i % 5 < 3:
l[idx] += df['col1'].iloc[i]
elif i % 5 == 3:
l[idx] = df['col1'].iloc[i]
else:
... | {
"problem_id": 80,
"library_problem_id": 80,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 76
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
l = []
for i in range(2 * (len(df) // 5) + (len(df) % 5) // 3 + 1):
l.append(0)
for i in range(len(df)):
idx = 2 * (i // 5) + (i % 5) /... |
Problem:
I have a simple dataframe which I would like to bin for every 3 rows to get sum and 2 rows to get avg from end to head.That means for the last 3 rows get their sum, then 2 rows get their avg, then 3 rows get their sum, then 2 rows get their avg…
It looks like this:
col1
0 2
1 1
2 3
3 ... | def g(df):
l = []
for i in range(2*(len(df) // 5) + (len(df) % 5) // 3 + 1):
l.append(0)
for i in reversed(range(len(df))):
idx = 2*((len(df)-1-i) // 5) + ((len(df)-1-i) % 5) // 3
if (len(df)-1-i) % 5 < 3:
l[idx] += df['col1'].iloc[i]
elif (len(df)-1-i) % 5 == 3:
... | {
"problem_id": 81,
"library_problem_id": 81,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 76
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
l = []
for i in range(2 * (len(df) // 5) + (len(df) % 5) // 3 + 1):
l.append(0)
for i in reversed(range(len(df))):
idx = 2 * ((len(df) ... |
Problem:
I have the following dataframe:
index = range(14)
data = [1, 0, 0, 2, 0, 4, 6, 8, 0, 0, 0, 0, 2, 1]
df = pd.DataFrame(data=data, index=index, columns = ['A'])
How can I fill the zeros with the previous non-zero value using pandas? Is there a fillna that is not just for "NaN"?.
The output should look like:
... | def g(df):
df['A'].replace(to_replace=0, method='ffill', inplace=True)
return df
df = g(df.copy())
| {
"problem_id": 82,
"library_problem_id": 82,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 82
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df["A"].replace(to_replace=0, method="ffill", inplace=True)
return df
def define_test_input(test_case_id):
if test_case_id == 1:
index = range... |
Problem:
I have the following dataframe:
index = range(14)
data = [1, 0, 0, 2, 0, 4, 6, 8, 0, 0, 0, 0, 2, 1]
df = pd.DataFrame(data=data, index=index, columns = ['A'])
How can I fill the zeros with the posterior non-zero value using pandas? Is there a fillna that is not just for "NaN"?.
The output should look like:... | def g(df):
df['A'].replace(to_replace=0, method='bfill', inplace=True)
return df
df = g(df.copy())
| {
"problem_id": 83,
"library_problem_id": 83,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 82
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df["A"].replace(to_replace=0, method="bfill", inplace=True)
return df
def define_test_input(test_case_id):
if test_case_id == 1:
index = range... |
Problem:
I have the following dataframe:
index = range(14)
data = [1, 0, 0, 2, 0, 4, 6, 8, 0, 0, 0, 0, 2, 1]
df = pd.DataFrame(data=data, index=index, columns = ['A'])
How can I fill the zeros with the maximun between previous and posterior non-zero value using pandas? Is there a fillna that is not just for "NaN"?. ... | def g(df):
l = df['A'].replace(to_replace=0, method='ffill')
r = df['A'].replace(to_replace=0, method='bfill')
for i in range(len(df)):
df['A'].iloc[i] = max(l[i], r[i])
return df
df = g(df.copy())
| {
"problem_id": 84,
"library_problem_id": 84,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 82
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
l = df["A"].replace(to_replace=0, method="ffill")
r = df["A"].replace(to_replace=0, method="bfill")
for i in range(len(df)):
df["A"].iloc[i] = max(... |
Problem:
This is my data frame
index duration
1 7 year
2 2day
3 4 week
4 8 month
I need to separate numbers from time and put them in two new columns.
I also need to create another column based on the values of time column. So the new dataset is like this:
index d... | def g(df):
df[['number','time']] = df.duration.str.extract(r'(\d+)\s*(.*)', expand=True)
df['time_days'] = df['time'].replace(['year', 'month', 'week', 'day'], [365, 30, 7, 1], regex=True)
return df
df = g(df.copy())
| {
"problem_id": 85,
"library_problem_id": 85,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 85
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df[["number", "time"]] = df.duration.str.extract(r"(\d+)\s*(.*)", expand=True)
df["time_days"] = df["time"].replace(
["year", "month", "week", "day"], [365... |
Problem:
This is my data frame
duration
1 year 7
2 day2
3 week 4
4 month 8
I need to separate numbers from time and put them in two new columns.
I also need to create another column based on the values of time column. So the new dataset is like this:
duration time number time_day
1 year 7 year ... | def g(df):
df[['time', 'number']] = df.duration.str.extract(r'\s*(.*)(\d+)', expand=True)
for i in df.index:
df.loc[i, 'time'] = df.loc[i, 'time'].strip()
df['time_days'] = df['time'].replace(['year', 'month', 'week', 'day'], [365, 30, 7, 1], regex=True)
return df
df = g(df.copy())
| {
"problem_id": 86,
"library_problem_id": 86,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 85
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df[["time", "number"]] = df.duration.str.extract(r"\s*(.*)(\d+)", expand=True)
for i in df.index:
df.loc[i, "time"] = df.loc[i, "time"].strip()
df[... |
Problem:
This is my data frame
index duration
1 7 year
2 2day
3 4 week
4 8 month
I need to separate numbers from time and put them in two new columns.
I also need to create another column based on the values of time column. So the new dataset is like this:
index d... | df[['number','time']] = df.duration.str.extract(r'(\d+)\s*(.*)', expand=True)
df['time_days'] = df['time'].replace(['year', 'month', 'week', 'day'], [365, 30, 7, 1], regex=True)
result = df
return result
| {
"problem_id": 87,
"library_problem_id": 87,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Surface",
"perturbation_origin_id": 85
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df[["number", "time"]] = df.duration.str.extract(r"(\d+)\s*(.*)", expand=True)
df["time_days"] = df["time"].replace(
["year", "month", "week", "day"], [365... |
Problem:
This is my data frame
duration
1 year 7
2 day2
3 week 4
4 month 8
I need to separate numbers from time and put them in two new columns.
I also need to create another column based on the values of time column. So the new dataset is like this:
duration time number time_day
1 year 7 year ... | def g(df):
df[['time', 'number']] = df.duration.str.extract(r'\s*(.*)(\d+)', expand=True)
for i in df.index:
df.loc[i, 'time'] = df.loc[i, 'time'].strip()
df.loc[i, 'number'] = eval(df.loc[i,'number'])
df['time_days'] = df['time'].replace(['year', 'month', 'week', 'day'], [365, 30, 7, 1], re... | {
"problem_id": 88,
"library_problem_id": 88,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 85
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df[["time", "number"]] = df.duration.str.extract(r"\s*(.*)(\d+)", expand=True)
for i in df.index:
df.loc[i, "time"] = df.loc[i, "time"].strip()
... |
Problem:
I am aware there are many questions on the topic of chained logical operators using np.where.
I have 2 dataframes:
df1
A B C D E F Postset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 no
2 1 2 3 4 5 6 yes
df2
A B C D E F Preset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 ... | def g(df1, df2, columns_check_list):
mask= (df1[columns_check_list] != df2[columns_check_list]).any(axis=1).values
return mask
result = g(df1, df2, columns_check_list)
| {
"problem_id": 89,
"library_problem_id": 89,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 89
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df1, df2, columns_check_list = data
mask = (df1[columns_check_list] != df2[columns_check_list]).any(axis=1).values
return mask
def define_test_input(tes... |
Problem:
I am aware there are many questions on the topic of chained logical operators using np.where.
I have 2 dataframes:
df1
A B C D E F Postset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 no
2 1 2 3 4 5 6 yes
df2
A B C D E F Preset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 ... | def g(df1, df2, columns_check_list):
mask= (df1[columns_check_list] == df2[columns_check_list]).any(axis=1).values
return mask
result = g(df1, df2, columns_check_list)
| {
"problem_id": 90,
"library_problem_id": 90,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Semantic",
"perturbation_origin_id": 89
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
data = data
df1, df2, columns_check_list = data
mask = (df1[columns_check_list] == df2[columns_check_list]).any(axis=1).values
return mask
def define_test_input(tes... |
Problem:
I have multi-index df as follows
x y
id date
abc 3/1/1994 100 7
9/1/1994 90 8
3/1/1995 80 9
Where dates are stored as str.
I want to parse date index. The following statement
df.index.levels[1] = pd.to_datetime(df.index.levels[1])
returns error:
TypeError: ... | def g(df):
df.index = df.index.set_levels([df.index.levels[0], pd.to_datetime(df.index.levels[1])])
return df
df = g(df.copy())
| {
"problem_id": 91,
"library_problem_id": 91,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 91
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.index = df.index.set_levels(
[df.index.levels[0], pd.to_datetime(df.index.levels[1])]
)
return df
def define_test_input(test_case_id):
... |
Problem:
I have multi-index df as follows
fee credits
name datetime
abc 3/1/1994 100 7
9/1/1994 90 8
3/1/1995 80 9
Where dates are stored as str.
I want to parse datetimw index. The following statement
df.index.levels[1] = pd.to_datetime(df.index.levels[1])
re... | def g(df):
df.index = df.index.set_levels([df.index.levels[0], pd.to_datetime(df.index.levels[1])])
return df
df = g(df.copy())
| {
"problem_id": 92,
"library_problem_id": 92,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Surface",
"perturbation_origin_id": 91
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.index = df.index.set_levels(
[df.index.levels[0], pd.to_datetime(df.index.levels[1])]
)
return df
def define_test_input(test_case_id):
... |
Problem:
I have multi-index df as follows
x y
id date
abc 3/1/1994 100 7
9/1/1994 90 8
3/1/1995 80 9
Where dates are stored as str.
I want to parse date index, and I want a numpy array of date, x and y as the output. Any help would be appreciated.
desired output:
[[Time... | df.index = df.index.set_levels([df.index.levels[0], pd.to_datetime(df.index.levels[1])])
df['date'] = sorted(df.index.levels[1].to_numpy())
df=df[['date', 'x', 'y']]
df = df.to_numpy()
return df
| {
"problem_id": 93,
"library_problem_id": 93,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 91
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
df.index = df.index.set_levels(
[df.index.levels[0], pd.to_datetime(df.index.levels[1])]
)
df["date"] = sorted(df.index.levels[1].to_numpy())
... |
Problem:
I have multi-index df as follows
x y
date id
3/1/1994 abc 100 7
9/1/1994 abc 90 8
3/1/1995 abc 80 9
Where dates are stored as str.
I want to parse date index using pd.to_datetime, and swap the two levels.
The final output should be
x y
id ... | df.index = df.index.from_tuples([(x[1], pd.to_datetime(x[0])) for x in df.index.values], names = [df.index.names[1], df.index.names[0]])
return df
| {
"problem_id": 94,
"library_problem_id": 94,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 91
} | 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.index = df.index.from_tuples(
[(x[1], pd.to_datetime(x[0])) for x in df.index.values],
names=[df.index.names[1], df.index.names[... |
Problem:
I have a data set which is in wide format like this
Index Country Variable 2000 2001 2002 2003 2004 2005
0 Argentina var1 12 15 18 17 23 29
1 Argentina var2 1 3 2 5 7 5
2 Brazil var1 20 23 25 29 31 32
3 Brazil var2 ... | def g(df):
return df.set_index(['Country', 'Variable']).rename_axis(['year'], axis=1).stack().unstack('Variable').reset_index()
df = g(df.copy())
| {
"problem_id": 95,
"library_problem_id": 95,
"library": "Pandas",
"test_case_cnt": 1,
"perturbation_type": "Origin",
"perturbation_origin_id": 95
} | 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.set_index(["Country", "Variable"])
.rename_axis(["year"], axis=1)
.stack()
.unstack("Variable")
.reset_... |
Problem:
I have a data set which is in wide format like this
Index Country Variable 2000 2001 2002 2003 2004 2005
0 Argentina var1 12 15 18 17 23 29
1 Argentina var2 1 3 2 5 7 5
2 Brazil var1 20 23 25 29 31 32
3 Brazil var2 ... | def g(df):
cols = list(df)[:2]+list(df)[-1:1:-1]
df = df.loc[:, cols]
return df.set_index(['Country', 'Variable']).rename_axis(['year'], axis=1).stack().unstack('Variable').reset_index()
df = g(df.copy())
| {
"problem_id": 96,
"library_problem_id": 96,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 95
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
cols = list(df)[:2] + list(df)[-1:1:-1]
df = df.loc[:, cols]
return (
df.set_index(["Country", "Variable"])
.rename_axis(["year"], axis... |
Problem:
I have a data frame like below
A_Name B_Detail Value_B Value_C Value_D ......
0 AA X1 1.2 0.5 -1.3 ......
1 BB Y1 0.76 -0.7 0.8 ......
2 CC Z1 0.7 -1.3 2.5 ......
3 DD L1 0.9 -0.5 0.4 ......... | def g(df):
mask = (df.filter(like='Value').abs() < 1).all(axis=1)
return df[mask]
df = g(df.copy())
| {
"problem_id": 97,
"library_problem_id": 97,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Origin",
"perturbation_origin_id": 97
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
mask = (df.filter(like="Value").abs() < 1).all(axis=1)
return df[mask]
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.Data... |
Problem:
I have a data frame like below
A_Name B_Detail Value_B Value_C Value_D ......
0 AA X1 1.2 0.5 -1.3 ......
1 BB Y1 0.76 -0.7 0.8 ......
2 CC Z1 0.7 -1.3 2.5 ......
3 DD L1 0.9 -0.5 0.4 ......... | def g(df):
mask = (df.filter(like='Value').abs() > 1).any(axis=1)
return df[mask]
df = g(df.copy())
| {
"problem_id": 98,
"library_problem_id": 98,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Semantic",
"perturbation_origin_id": 97
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
mask = (df.filter(like="Value").abs() > 1).any(axis=1)
return df[mask]
def define_test_input(test_case_id):
if test_case_id == 1:
df = pd.Data... |
Problem:
I have a data frame like below
A_Name B_Detail Value_B Value_C Value_D ......
0 AA X1 1.2 0.5 -1.3 ......
1 BB Y1 0.76 -0.7 0.8 ......
2 CC Z1 0.7 -1.3 2.5 ......
3 DD L1 0.9 -0.5 0.4 ......... | def g(df):
mask = (df.filter(like='Value').abs() > 1).any(axis=1)
cols = {}
for col in list(df.filter(like='Value')):
cols[col]=col.replace("Value_","")
df.rename(columns=cols, inplace=True)
return df[mask]
df = g(df.copy())
| {
"problem_id": 99,
"library_problem_id": 99,
"library": "Pandas",
"test_case_cnt": 2,
"perturbation_type": "Difficult-Rewrite",
"perturbation_origin_id": 97
} | import pandas as pd
import numpy as np
import copy
def generate_test_case(test_case_id):
def generate_ans(data):
df = data
mask = (df.filter(like="Value").abs() > 1).any(axis=1)
cols = {}
for col in list(df.filter(like="Value")):
cols[col] = col.replace("Value_", "")
... |
Subsets and Splits
Random Pandas Questions & Solutions
Retrieves a random sample of 100 questions and their corresponding solutions from the dataset where the library used is Pandas, providing a basic overview of the types of questions and solutions available.
Random Pandas Code Prompts
Randomly selects 100 questions and their corresponding solutions from the dataset where the library is Pandas, providing a basic sample of the data.
Random Pandas Code Prompts
Randomly selects 150 questions and their corresponding solutions from the dataset where the library is Pandas, providing a basic sample of the data.
SQL Console for xlangai/DS-1000
Filters records that contain 'Scipy' in their metadata, providing a limited view of relevant entries.