【问题标题】:how to fill rows with help of index pandas?如何在索引熊猫的帮助下填充行?
【发布时间】:2021-07-19 17:21:15
【问题描述】:

如何在 index pandas 的帮助下填充行?

我有类似的数据框

    Alert number    Age Job Loan
0        0          58   4   0
2        2          44   9   0
4        4          35   4   0
6        6          41   0   0
8        8          29   0   0

我有另一个具有相同列数和不同索引值的数据框,例如

     Alert number   Age Job Loan
1        1          58   4   0
3        3          44   9   0
5        5          35   4   0
7        7          41   0   0
9        9          29   0   0

pandas中如何将以上两行顺序组合?

像下面的数据框

    Alert number    Age Job Loan
0        0          58   4   0
1        1          58   4   0
2        2          44   9   0
3        3          44   9   0
4        4          35   4   0
5        5          35   4   0
6        6          41   0   0
7        7          41   0   0
8        8          29   0   0
9        9          29   0   0

【问题讨论】:

    标签: python pandas dataframe indexing sklearn-pandas


    【解决方案1】:

    使用concat 并按DataFrame.sort_values 排序:

    df = pd.concat([df1, df2]).sort_values('Alert number', ignore_index=True)
    

    DataFrame.sort_index:

    df = pd.concat([df1, df2]).sort_index()
    

    【讨论】:

    • 我的问题是我有两个数据框,其中一个数据框有大约 7000 行,另一个有 18000 行。包含 7000 行的第一个数据帧包含在 18000 行中,但唯一的区别是 7000 行是异常值。所以我想用额外的列将异常值添加到我的主要数据中。我正在编辑我的问题,请检查
    • @NithinReddy - 我看不到 EDIT,但可能需要 df2.update(df1)
    猜你喜欢
    • 2018-03-25
    • 2021-12-30
    • 2019-07-12
    • 2021-09-26
    • 1970-01-01
    • 2018-07-17
    • 2013-04-05
    • 2017-08-04
    • 2020-08-31
    相关资源
    最近更新 更多