【问题标题】:To insert lists into rows at a particular index position and update index pandas python将列表插入到特定索引位置的行中并更新索引 pandas python
【发布时间】:2021-03-25 17:29:43
【问题描述】:

我有一个数据框和两个列表newx newy。所以我想在特定的索引位置插入这两个列表。 我的数据框如下所示,第一列代表索引 我的两个清单是

newx = [492, 491]
newy = [260, 247]

我想在第 1 列的索引 595 和 596 处插入 newx 列表和第 2 列 newy 列表并更新剩余的索引 所以我的输出数据框应该看起来像

【问题讨论】:

    标签: python pandas dataframe insert-update


    【解决方案1】:
    # df is the big existing dataframe
    # splitting dataframe by row index 
    df_1 = df.iloc[:594,:].reset_index() 
    df_3 = df.iloc[595:,:].reset_index()
    
    df_2 = pd.DataFrame({'newx' :[492, 491], 'newy' : [260, 247]})
    
    frames = [df_1, df_2, df_3]
    
    result = pd.concat(frames)
    

    因此,如果您要插入数据框,请将其分成两部分,然后制作您要插入的部分。然后加入他们。

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 2021-12-27
      • 2013-01-31
      • 2016-08-16
      相关资源
      最近更新 更多