Python 使用所需重复的列表将重复行插入数据帧
我正在尝试使用另一个数据帧的索引复制一个数据帧。例如,想象以下情况:Python 使用所需重复的列表将重复行插入数据帧,python,pandas,Python,Pandas,我正在尝试使用另一个数据帧的索引复制一个数据帧。例如,想象以下情况: basket = pd.DataFrame(columns = ["food_type", "food", "qty"], data=[ ["fruit" , "apple", 1], ["fruit" , "pear", 1], ["fruit" , "banana", 1], ["veggie", "carrot", 1], ["veggie", "lettu
basket = pd.DataFrame(columns = ["food_type", "food", "qty"], data=[
["fruit" , "apple", 1],
["fruit" , "pear", 1],
["fruit" , "banana", 1],
["veggie", "carrot", 1],
["veggie", "lettuce", 1]])
basket.set_index(["food_type", "food"], inplace=True)
target_df = pd.DataFrame(columns = ["person", "food_type", "food", "qty"]
).set_index(["person", "food_type", "food"])
people = ["jane", "john", "joan"]
for person in people:
basket_copy = basket.copy()
basket_copy["person"] = person
basket_copy.set_index("person", append=True, inplace=True)
target_df = target_df.append(basket_copy)
我正在尝试使用人员列表复制这些行,以便实现以下目标:
basket = pd.DataFrame(columns = ["food_type", "food", "qty"], data=[
["fruit" , "apple", 1],
["fruit" , "pear", 1],
["fruit" , "banana", 1],
["veggie", "carrot", 1],
["veggie", "lettuce", 1]])
basket.set_index(["food_type", "food"], inplace=True)
target_df = pd.DataFrame(columns = ["person", "food_type", "food", "qty"]
).set_index(["person", "food_type", "food"])
people = ["jane", "john", "joan"]
for person in people:
basket_copy = basket.copy()
basket_copy["person"] = person
basket_copy.set_index("person", append=True, inplace=True)
target_df = target_df.append(basket_copy)
这个解决方案可行,但看起来很笨拙。在Python中有没有更自然的方法来实现这一点?我正在想象篮子数据框和一组人之间的某种合并。您可以合并,并且:
结果输出:
qty
food_type food person
fruit apple jane 1
pear jane 1
banana jane 1
veggie carrot jane 1
lettuce jane 1
fruit apple john 1
pear john 1
banana john 1
veggie carrot john 1
lettuce john 1
fruit apple joan 1
pear joan 1
banana joan 1
veggie carrot joan 1
lettuce joan 1
您可以组合,并且:
结果输出:
qty
food_type food person
fruit apple jane 1
pear jane 1
banana jane 1
veggie carrot jane 1
lettuce jane 1
fruit apple john 1
pear john 1
banana john 1
veggie carrot john 1
lettuce john 1
fruit apple joan 1
pear joan 1
banana joan 1
veggie carrot joan 1
lettuce joan 1
构造pd.MultiIndex+np.repeat
另一个pd.concat选项
虽然没有那么干净
两者都屈服
朴素时间测试
过度给定的数据
构造pd.MultiIndex+np.repeat
另一个pd.concat选项
虽然没有那么干净
两者都屈服
朴素时间测试
过度给定的数据
人和食物被交换…人和食物被交换…接受另一个答案,因为代码稍微短一点,但我喜欢使用np.repeat的想法。非常感谢。接受另一个答案,因为代码略短,但我喜欢使用np.repeat。非常感谢。
qty
food_type food person
fruit apple jane 1
john 1
joan 1
banana jane 1
john 1
joan 1
pear jane 1
john 1
joan 1
veggie carrot jane 1
john 1
joan 1
lettuce jane 1
john 1
joan 1