Pandas数据类型-DataFrame数据编辑

编辑数据列

直接赋值

  • df[]=,当赋单值时,会自动增加一列
  • df.loc[]=
import pandas as pd
df1 = pd.read_excel(r".\study\test_excel.xlsx", sheet_name="student")
print("df1---------------\n", df1, type(df1))
df1---------------
   name  age sex address  score
0   刘一   18   女      上海    100
1   花二   40   男      上海     99
2   张三   25   男      北京     80
3   李四   30   男      西安     40
4   王五   70   男      青岛     70
5   孙六   65   女      泰州     90 <class 'pandas.core.frame.DataFrame'>
df1["class"]= [1,2,3,4,5,6]
df1.loc[:, "grade"]= ["A", "B", "C", "D", "E", "F"]
print(df1)
df1["school"] = 5
print(df1)
  name  age sex address  score  class grade
0   刘一   18   女      上海    100      1     A
1   花二   40   男      上海     99      2     B
2   张三   25   男      北京     80      3     C
3   李四   30   男      西安     40      4     D
4   王五   70   男      青岛     70      5     E
5   孙六   65   女      泰州     90      6     F
  name  age sex address  score  class grade  school
0   刘一   18   女      上海    100      1     A       5
1   花二   40   男      上海     99      2     B       5
2   张三   25   男      北京     80      3     C       5
3   李四   30   男      西安     40      4     D       5
4   王五   70   男      青岛     70      5     E       5
5   孙六   65   女      泰州     90      6     F       5

loc 方法

  • loc 方法和 iloc 方法一样,可以索引 DataFrame 数据,一般是通过 data.loc[index, col] = value 来进行赋值.
  • 可以利用:来索引全部行再进行赋值。
  • 当赋单值时,会自动增加一列
df1 = pd.DataFrame([1, 2, 3],["a", "b", "c"], columns=["first"])
print(df1)
df1.loc[:, 'second'] = 0
df1
   first
a      1
b      2
c      3
firstsecond
a10
b20
c30

insert方法

  • insert(loc, column, value, allow_duplicates=False)
  • loc: 要插入的位置
  • column: 插入列的列名
  • value: 插入列的数据
  • allow_duplicates: DataFrame默认是不允许添加重复的列的,如果想添加,需要修改allow_duplicates值,默认为False
df1 = pd.read_excel(r".\study\test_excel.xlsx", sheet_name="student")
print("df1---------------\n", df1, type(df1))
df1.insert(df1.shape[1], 'class', 3, allow_duplicates=False)
df1
df1---------------
   name  age sex address  score
0   刘一   18   女      上海    100
1   花二   40   男      上海     99
2   张三   25   男      北京     80
3   李四   30   男      西安     40
4   王五   70   男      青岛     70
5   孙六   65   女      泰州     90 <class 'pandas.core.frame.DataFrame'>
nameagesexaddressscoreclass
0刘一18上海1003
1花二40上海993
2张三25北京803
3李四30西安403
4王五70青岛703
5孙六65泰州903

concat 方法

  • concat方法是用来拼接的,同一类型,形成新的数据
  • 如果列名相同或者未指定列名,则会向下拼接为多行的数据
  • 如果指定了不同列名,则列也会拓展
# 相同列名
df1 = pd.DataFrame([1, 2, 3],["a", "b", "c"], columns=["第1列"])
df2 = pd.DataFrame([4, 5, 6],["x", "y", "z"], columns=["第1列"])
pd.concat([df1, df2], sort=False)
第1列
a1
b2
c3
x4
y5
z6
# 不同列名
df1 = pd.DataFrame([1, 2, 3],["a", "b", "c"], columns=["first"])
df2 = pd.DataFrame([4, 5, 6],["x", "y", "z"], columns=["second"])
df3 = pd.concat([df1, df2], sort=False)
df3
firstsecond
a1.0NaN
b2.0NaN
c3.0NaN
xNaN4.0
yNaN5.0
zNaN6.0

dataframe.assign()

  • 可以进行多列添加
  • Dataframe.assign()返回一个新对象,其包含新列,还包含所有原始列。重新分配的现有列将被覆盖
  • DataFrame.assign(**kwargs) dict of {str: callable or Series}
  • key是列名。如果value是callable,则参数是DataFrame,计算结果将分配给新列。callable对象不能更改输入DataFrame(尽管pandas不检查它)。如果value不是callable(例如Series、标量或数组),则只对其赋值
df1 = pd.DataFrame({'temp_c': [17.0, 25.0]},
                  index=['Portland', 'Berkeley'])
df1
temp_c
Portland17.0
Berkeley25.0
df2 = df1.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
print("df2----------", df2)
# the same behavior can be achieved by directly referencing an existing Series or sequence:
df4 = df1.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,
          temp_k=lambda x: (x['temp_f'] +  459.67) * 5 / 9)
print("df4----------", df4)
df5 = df1.assign(air_quality=[20, 50], pollution_index=[70, 80])
print("df5----------", df5)
df2----------           temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0
df4----------           temp_c  temp_f  temp_k
Portland    17.0    62.6  290.15
Berkeley    25.0    77.0  298.15
df5----------           temp_c  air_quality  pollution_index
Portland    17.0           20               70
Berkeley    25.0           50               80

编辑数据行

loc直接赋值

  • 注意可以省略列,也可以用:
  • 如果loc[index]中的index存在,那么就会替换原本index上的值
df1 = pd.DataFrame({'one':[1, 2, 3, 4], 'two':[11, 22, 33, 44]})
new_index = [66, 88]
df1.loc[4, :] = new_index  # df1.loc[4]
print(df1)

print(df1.loc[0:4, ['one', 'two']])
df1.loc[0:1, :]=[[30,50],[70,80]]    #修改多行
print(df1)
    one   two
0   1.0  11.0
1   2.0  22.0
2   3.0  33.0
3   4.0  44.0
4  66.0  88.0
    one   two
0   1.0  11.0
1   2.0  22.0
2   3.0  33.0
3   4.0  44.0
4  66.0  88.0
    one   two
0  30.0  50.0
1  70.0  80.0
2   3.0  33.0
3   4.0  44.0
4  66.0  88.0

at直接赋值

df1.at[2021, :] = [100, 200]
df1.at[2022, :] = 88
df1
onetwo
030.050.0
130.050.0
23.033.0
34.044.0
466.088.0
2021100.0200.0
202288.088.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值