📜  Python| pandas.merge_ordered()函数

📅  最后修改于: 2022-05-13 01:55:13.241000             🧑  作者: Mango

Python| pandas.merge_ordered()函数

该方法用于为时间序列数据等有序数据设计。可选地执行 group-wise merge

示例 1:合并有序的两个具有相同元素数量的 Dataframe

Python3
# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({
    "date": ['2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01', '2007-06-01'],
    "close": [12.08, 13.27, 14.27, 17.31, 17.43]
})
print("The first DataFrame")
print(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({
    "date": ['2007-01-01', '2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01'],
    "close": [44.34, 43.68, 45.04, 48.27, 50.54]
})
print("The second DataFrame")
print(df2)
  
# merging the DataFrames
print("The merge_ordered DataFrame")
  
df = pd.merge_ordered(df1, df2, on='date', suffixes=('_df1', '_df2'))
  
print(df)
# This code is contributed by rakeshsahni


Python3
# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({
    "date": ['2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01', '2007-06-01'],
    "close": [12.08, 13.27, 14.27, 17.31, 17.43]
})
print("The first DataFrame")
print(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({
    "date": ['2007-01-01', '2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01'],
    "close": [44.34, 43.68, 45.04, 48.27, 50.54]
})
print("The second DataFrame")
print(df2)
  
# merging the DataFrames
print("The merge_ordered DataFrame")
  
df = pd.merge_ordered(df1, df2, on='date', suffixes=(
    '_df1', '_df2'), fill_method='ffill')
  
print(df)
# This code is contributed by rakeshsahni


Python3
# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame(
    {
        "key": ["k1", "k3", "k5", "k1", "k3", "k5"],
        "value1": [1, 2, 3, 1, 2, 3],
        "gp": ["g1", "g1", "g1", "g2", "g2", "g2"]
    }
)
df1
print("The first DataFrame")
print(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"key": ["k2", "k3", "k4"], "value2": [1, 2, 3]})
  
print("The second DataFrame")
print(df2)
  
# merging the DataFrames
print("The merge_ordered DataFrame")
  
df = pd.merge_ordered(df1,df2,fill_method='ffill',left_by="gp")
  
print(df)
# This code is contributed by rakeshsahni


输出 :




示例 2:使用之前的值填充缺失值,我们使用 fill_method = 'ffill'(前向填充)

蟒蛇3

# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame({
    "date": ['2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01', '2007-06-01'],
    "close": [12.08, 13.27, 14.27, 17.31, 17.43]
})
print("The first DataFrame")
print(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({
    "date": ['2007-01-01', '2007-02-01', '2007-03-01', '2007-04-01', '2007-05-01'],
    "close": [44.34, 43.68, 45.04, 48.27, 50.54]
})
print("The second DataFrame")
print(df2)
  
# merging the DataFrames
print("The merge_ordered DataFrame")
  
df = pd.merge_ordered(df1, df2, on='date', suffixes=(
    '_df1', '_df2'), fill_method='ffill')
  
print(df)
# This code is contributed by rakeshsahni

输出 :


示例 3:我们将使用 left_by 参数,其中将左侧 DataFrame 按列分组并与右侧 DataFrame 逐个合并。

蟒蛇3

# importing the module
import pandas as pd
  
# creating the first DataFrame
df1 = pd.DataFrame(
    {
        "key": ["k1", "k3", "k5", "k1", "k3", "k5"],
        "value1": [1, 2, 3, 1, 2, 3],
        "gp": ["g1", "g1", "g1", "g2", "g2", "g2"]
    }
)
df1
print("The first DataFrame")
print(df1)
  
# creating the second DataFrame
df2 = pd.DataFrame({"key": ["k2", "k3", "k4"], "value2": [1, 2, 3]})
  
print("The second DataFrame")
print(df2)
  
# merging the DataFrames
print("The merge_ordered DataFrame")
  
df = pd.merge_ordered(df1,df2,fill_method='ffill',left_by="gp")
  
print(df)
# This code is contributed by rakeshsahni

输出 :