Pandas-数据结构-DataFrame(七):添加元素、修改元素、删除元素

Pandas-数据结构-DataFrame(七):添加元素、修改元素、删除元素,第1张

一、添加元素

新增列/行并赋值

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                  columns=['a', 'b', 'c', 'd'])
print("df = \n", df)
print("-" * 100)

# 新增列/行并赋值
df['e'] = 10
df.loc[4] = 20
print("df = \n", df)

打印结果:

df = 
            a          b          c          d
0  39.621173  36.363400  21.042923  36.160210
1  39.775787  32.010771  86.375488  15.035171
2  49.328983  58.402799  12.979410  59.408702
3  21.167906  91.684124  22.953356  98.618665
----------------------------------------------------------------------------------------------------
df = 
            a          b          c          d   e
0  39.621173  36.363400  21.042923  36.160210  10
1  39.775787  32.010771  86.375488  15.035171  10
2  49.328983  58.402799  12.979410  59.408702  10
3  21.167906  91.684124  22.953356  98.618665  10
4  20.000000  20.000000  20.000000  20.000000  20
二、修改元素
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                  columns=['a', 'b', 'c', 'd'])
print("df = \n", df)
print("-" * 100)

# 索引后直接修改值
df[['a', 'c']] = 100
print("df = \n", df)

打印结果:

df = 
            a          b          c          d
0  39.621173  36.363400  21.042923  36.160210
1  39.775787  32.010771  86.375488  15.035171
2  49.328983  58.402799  12.979410  59.408702
3  21.167906  91.684124  22.953356  98.618665
----------------------------------------------------------------------------------------------------
df = 
      a          b    c          d   e
0  100  36.363400  100  36.160210  10
1  100  32.010771  100  15.035171  10
2  100  58.402799  100  59.408702  10
3  100  91.684124  100  98.618665  10
4  100  20.000000  100  20.000000  20
三、删除:del / drop()

删除:del / drop()

  • del:为原地删除
  • df.drop([1, 2]:返回新对象,其中的索引为名词索引而非位置下标索引
import numpy as np
import pandas as pd

# 删除  del / drop()

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                  index=[2, 1, 3, 0],
                  columns=['a', 'b', 'c', 'd'])
print("df = \n", df)
print("-" * 50)

# del语句 - 删除列
del df['a']
print("删除第a列后:df = \n", df)
print("-" * 100)

# drop()删除行,inplace=False → 删除后生成新的数据,不改变原数据
df2 = df.drop(0)
df3 = df.drop([1, 2])
print("df = \n", df)
print("-" * 50)
print("删除第0行后:df2 = \n", df2)
print("-" * 50)
print("删除第1、2行后:df3 = \n", df3)
print("-" * 100)

# drop()删除列,需要加上axis = 1,inplace=False → 删除后生成新的数据,不改变原数据
df4 = df.drop(['d'], axis=1)
print("df = \n", df)
print("-" * 50)
print("df4 = \n", df4)

打印结果:

df = 
            a          b          c          d
2  97.538939  28.115040  25.287657  82.030868
1  58.227305  40.422558  58.629622  19.732153
3  39.179936  57.157078  33.694614  68.138918
0  57.069357  35.340227  91.023129  69.587282
--------------------------------------------------
删除第a列后:df = 
            b          c          d
2  28.115040  25.287657  82.030868
1  40.422558  58.629622  19.732153
3  57.157078  33.694614  68.138918
0  35.340227  91.023129  69.587282
----------------------------------------------------------------------------------------------------
df = 
            b          c          d
2  28.115040  25.287657  82.030868
1  40.422558  58.629622  19.732153
3  57.157078  33.694614  68.138918
0  35.340227  91.023129  69.587282
--------------------------------------------------
删除第0行后:df2 = 
            b          c          d
2  28.115040  25.287657  82.030868
1  40.422558  58.629622  19.732153
3  57.157078  33.694614  68.138918
--------------------------------------------------
删除第12行后:df3 = 
            b          c          d
3  57.157078  33.694614  68.138918
0  35.340227  91.023129  69.587282
----------------------------------------------------------------------------------------------------
df = 
            b          c          d
2  28.115040  25.287657  82.030868
1  40.422558  58.629622  19.732153
3  57.157078  33.694614  68.138918
0  35.340227  91.023129  69.587282
--------------------------------------------------
df4 = 
            b          c
2  28.115040  25.287657
1  40.422558  58.629622
3  57.157078  33.694614
0  35.340227  91.023129

Process finished with exit code 0

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/662398.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)

保存