Python Pandas 修改表格数据类型 DataFrame 列的顺序案例

目录

一、修改表格数据类型 DataFrame 列的顺序

实战场景:Pandas 如何修改表格数据类型 DataFrame 列的顺序

1.1主要知识点 文件读写 基础语法 数据构建 Pandas Numpy

实战:

1.2创建 python 文件
import numpy as np
import pandas as pd

np.random.seed(66)
df = pd.DataFrame(np.random.rand(10, 4), columns=list('ABCD'))
print(df)
df = df[["D", "A", "B", "C"]]
print(df)

1.3运行结果 

          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290953  0.298641  0.031346  0.678006  0.9034894  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710438  0.049213  0.465223  0.941233  0.2165129  0.361318  0.031319  0.304045  0.188268          D         A         B         C0  0.679109  0.154288  0.133700  0.3626851  0.557619  0.194450  0.251210  0.7584162  0.829095  0.514803  0.467800  0.0871763  0.903489  0.298641  0.031346  0.6780064  0.634057  0.514451  0.539105  0.6643285  0.879319  0.353419  0.026643  0.1652906  0.096294  0.067820  0.369086  0.1155017  0.771043  0.083770  0.086927  0.0222568  0.216512  0.049213  0.465223  0.9412339  0.188268  0.361318  0.031319  0.304045

二、Pandas 如何统计某个数据列的空值个数

实战场景:Pandas 如何统计某个数据列的空值个数

2.1主要知识点 文件读写 基础语法 Pandas numpy

实战:

2.2创建 python 文件
"""
对如下DF,设置两个单元格的值
·使用iloc 设置(3,B)的值是nan
·使用loc设置(8,D)的值是nan
"""
import numpy as np
import pandas as pd
np.random.seed(66)
df = pd.DataFrame(np.random.rand(10, 4), columns=list('ABCD'))
df.iloc[3, 1] = np.nan
df.loc[8, 'D'] = np.nan
print(df)
print(df.isnull().sum())

2.3运行结果

          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290953  0.298641       NaN  0.678006  0.9034894  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710438  0.049213  0.465223  0.941233       NaN9  0.361318  0.031319  0.304045  0.188268A    0B    1C    0D    1dtype: int64

三、Pandas如何移除包含空值的行

实战场景:Pandas如何移除包含空值的行

3.1主要知识点 文件读写 基础语法 Pandas numpy

实战:

3.2创建 python 文件
"""
对如下DF,设置两个单元格的值
·使用iloc 设置(3,B)的值是nan
·使用loc设置(8,D)的值是nan
"""
import numpy as np
import pandas as pd
 
np.random.seed(66)
df = pd.DataFrame(np.random.rand(10, 4), columns=list('ABCD'))
df.iloc[3, 1] = np.nan
df.loc[8, 'D'] = np.nan
print(df)
df2 = df.dropna()
print(df2)

3.3运行结果

          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290953  0.298641       NaN  0.678006  0.9034894  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710438  0.049213  0.465223  0.941233       NaN9  0.361318  0.031319  0.304045  0.188268          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290954  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710439  0.361318  0.031319  0.304045  0.188268

四、Pandas如何精确设置表格数据的单元格的值

实战场景:Pandas如何精确设置表格数据的单元格的值

4.1主要知识点 文件读写 基础语法 Pandas numpy

实战:

4.2创建 python 文件
"""
对如下DF,设置两个单元格的值
·使用iloc 设置(3,B)的值是nan
·使用loc设置(8,D)的值是nan
"""
import numpy as np
import pandas as pd
np.random.seed(66)
df = pd.DataFrame(np.random.rand(10, 4), columns=list('ABCD'))
print(df)
 
df.iloc[3, 1] = np.nan
df.loc[8, 'D'] = np.nan
 
print(df)

4.3运行结果 

          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290953  0.298641  0.031346  0.678006  0.9034894  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710438  0.049213  0.465223  0.941233  0.2165129  0.361318  0.031319  0.304045  0.188268          A         B         C         D0  0.154288  0.133700  0.362685  0.6791091  0.194450  0.251210  0.758416  0.5576192  0.514803  0.467800  0.087176  0.8290953  0.298641       NaN  0.678006  0.9034894  0.514451  0.539105  0.664328  0.6340575  0.353419  0.026643  0.165290  0.8793196  0.067820  0.369086  0.115501  0.0962947  0.083770  0.086927  0.022256  0.7710438  0.049213  0.465223  0.941233       NaN9  0.361318  0.031319  0.304045  0.188268 

原文地址:https://blog.csdn.net/qq_39816613/article/details/126135559
29人参与, 0条评论 登录后显示评论回复

你需要登录后才能评论 登录/ 注册