将一个DataFrame中的一列(行),插入到另一个DataFrame中

原始数据:

import pandas as pd
import numpy as np

data = {'a': [4, 6, 5, 7, 8],
        'b': ['w', 't', 'y', 'x', 'z'],
        'c': [1, 0, 6, -5, 3],
        'd': [3, 4, 7, 10, 8],
        }
df = pd.DataFrame(data, index=['one', 'two', 'three', 'four', 'five'])
print(df)

#        a  b  c   d
# one    4  w  1   3
# two    6  t  0   4
# three  5  y  6   7
# four   7  x -5  10
# five   8  z  3   8

new_df = pd.DataFrame({'a': [1, 2, 3, 3, 4],
                       'b': [1, 2, 3, 3, 4],
                       'c': [22, 33, 22, 44, 66],
                       'd': [1, 2, 3, 3, 4]
                       },
                      index=['one', 'two', 'three', 'four', 'five'])
print(new_df)

#        a  b   c  d
# one    1  1  22  1
# two    2  2  33  2
# three  3  3  22  3
# four   3  3  44  3
# five   4  4  66  4

插入列

方法一
new_df['newcol'] = df['b']
print(new_df)

#        a  b   c  d newcol
# one    1  1  22  1      w
# two    2  2  33  2      t
# three  3  3  22  3      y
# four   3  3  44  3      x
# five   4  4  66  4      z
方法二
new_df['newcol'] = df.pop('b')
print(new_df)

#        a  b   c  d newcol
# one    1  1  22  1      w
# two    2  2  33  2      t
# three  3  3  22  3      y
# four   3  3  44  3      x
# five   4  4  66  4      z
方法三
new_df.insert(2, 'newcol', df['b'].values)
print(new_df)

#        a  b newcol   c  d
# one    1  1      w  22  1
# two    2  2      t  33  2
# three  3  3      y  22  3
# four   3  3      x  44  3
# five   4  4      z  66  4
insert方法详解

DataFrame.insert(loc, column, value, allow_duplicates=False)

Parameters:

  • loc : 参数column插入的位置,如果想插入到第一例则为0,取值范围: 0 <= loc <= len(columns),其中len(columns)为Dataframe的列数
  • column :给 插入数据value取列名,可为数字,字符串等
  • value : 可以是整数,Series或者数组等
  • allow_duplicates : 默认 False。意为:如果插入的列已经存在,则抛出异常

三种方法的异同点:

同:都可以实现将一个 DataFrame 中的一列,插入到另一个 DataFrame 中
异:方法一和方法二,必须保证两个 DataFrame 的 index 要相同,若不相同,则这一列对应的值为NaN,而且总是在末尾插入列。
    而方法三没有这个限制,可以放在任意列,也不要求 index 相同

插入行:

方法一:直接使用append
# 取出要插入的行
insertRow = df[1: 2]
# insertRow = df.iloc[2, :]    # 切片操作,行取第二行,列取所有
# insertRow = df.iloc[2]    # 第二行,返回位置索引为1,也就是第二行数据。位置索引,和列表索引类似,里面只能是数字
# insertRow = df.loc['two']    # 返回标签为‘two’的数据
print(insertRow)

#      a  b  c  d
# two  6  t  0  4

newData = new_df.append(insertRow)
print(newData)

#        a  b   c  d
# one    1  1  22  1
# two    2  2  33  2
# three  3  3  22  3
# four   3  3  44  3
# five   4  4  66  4
# two    6  t   0  4
方法二:插入行数据,前提是要插入的这一行的值的个数能与dataframe中的列数对应且列名相同,思路:先切割,再拼接。
# 将要插入的这一行的列名,要与被插入的DataFrame的列名相同,若不相同,则会增加某一的列,对应的值为NaN


# 先切割
above = new_df[:2]
print(above)

#      a  b   c  d
# one  1  1  22  1
# two  2  2  33  2

below = new_df[2:]
print(below)

#        a  b   c  d
# three  3  3  22  3
# four   3  3  44  3
# five   4  4  66  4

# 再拼接
newData = above.append(insertRow, ignore_index=True).append(below, ignore_index=True)
print(newData)

#    a  b   c  d
# 0  1  1  22  1
# 1  2  2  33  2
# 2  6  t   0  4
# 3  3  3  22  3
# 4  3  3  44  3
# 5  4  4  66  4

两种方法的异同点:

同:都可以实现将一个 DataFrame 中的一行,插入到另一个 DataFrame 中
异:方法一总是在末尾插入列。
    而方法三没有这个限制,可以放在任意列
loc和iloc的区别
  • loc works on labels in the index.(说白了就是标签索引)
  • iloc works on the positions in the index (so it only takes integers). (位置索引,和列表索引类似,里面只能是数字)
  • 21
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值