【发布时间】:2019-03-15 23:49:03
【问题描述】:
给定一个数据框列表,我想迭代地合并它们并返回单个数据框。输入:frames(pandas 数据框列表)和on_columns(包含要合并的列名的字符串或字符串列表)。如何使用df.merge 来完成此操作?
"""
给定一个数据框列表,迭代合并它们并返回一个
单个数据框
"""HINT: Use slice on frames when iterating and merging.
Arguments:
frames {list} -- a list of pandas DataFrames
on_columns {string or list} -- a string or list of strings
containing the column names on which to join
Returns:
df -- a pandas.DataFrame containing a merged version of the
two provided dataframes. If frames is None or an empty list return None
"""
def merge(frames, on_columns):
#implementation here
df = #merged df
return df
编辑:我想也许我可以使用 df.concat 但不确定如何使用?
【问题讨论】:
-
在每种情况下,on 列是否相同?似乎
reduce的情况为answered here -
类似,但我想合并
on_columns中提供的所有列,而不仅仅是彼此不同的列。 -
您能否提供minimal reproducible example 一些示例数据和您的预期输出?
-
我编辑了它 - 希望能有所帮助。