Python?Pandas?修改表格數(shù)據(jù)類型?DataFrame?列的順序案例
一、修改表格數(shù)據(jù)類型 DataFrame 列的順序
實戰(zhàn)場景:Pandas 如何修改表格數(shù)據(jù)類型 DataFrame 列的順序
1.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- 數(shù)據(jù)構(gòu)建
- Pandas
- Numpy
實戰(zhàn):
1.2創(chuàng)建 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運行結(jié)果
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
3 0.298641 0.031346 0.678006 0.903489
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
8 0.049213 0.465223 0.941233 0.216512
9 0.361318 0.031319 0.304045 0.188268
D A B C
0 0.679109 0.154288 0.133700 0.362685
1 0.557619 0.194450 0.251210 0.758416
2 0.829095 0.514803 0.467800 0.087176
3 0.903489 0.298641 0.031346 0.678006
4 0.634057 0.514451 0.539105 0.664328
5 0.879319 0.353419 0.026643 0.165290
6 0.096294 0.067820 0.369086 0.115501
7 0.771043 0.083770 0.086927 0.022256
8 0.216512 0.049213 0.465223 0.941233
9 0.188268 0.361318 0.031319 0.304045
二、Pandas 如何統(tǒng)計某個數(shù)據(jù)列的空值個數(shù)
實戰(zhàn)場景:Pandas 如何統(tǒng)計某個數(shù)據(jù)列的空值個數(shù)
2.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
實戰(zhàn):
2.2創(chuàng)建 python 文件
"""
對如下DF,設(shè)置兩個單元格的值
·使用iloc 設(shè)置(3,B)的值是nan
·使用loc設(shè)置(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運行結(jié)果
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
3 0.298641 NaN 0.678006 0.903489
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
8 0.049213 0.465223 0.941233 NaN
9 0.361318 0.031319 0.304045 0.188268
A 0
B 1
C 0
D 1
dtype: int64
三、Pandas如何移除包含空值的行
實戰(zhàn)場景:Pandas如何移除包含空值的行
3.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
實戰(zhàn):
3.2創(chuàng)建 python 文件
"""
對如下DF,設(shè)置兩個單元格的值
·使用iloc 設(shè)置(3,B)的值是nan
·使用loc設(shè)置(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運行結(jié)果
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
3 0.298641 NaN 0.678006 0.903489
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
8 0.049213 0.465223 0.941233 NaN
9 0.361318 0.031319 0.304045 0.188268
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
9 0.361318 0.031319 0.304045 0.188268
四、Pandas如何精確設(shè)置表格數(shù)據(jù)的單元格的值
實戰(zhàn)場景:Pandas如何精確設(shè)置表格數(shù)據(jù)的單元格的值
4.1主要知識點
- 文件讀寫
- 基礎(chǔ)語法
- Pandas
- numpy
實戰(zhàn):
4.2創(chuàng)建 python 文件
"""
對如下DF,設(shè)置兩個單元格的值
·使用iloc 設(shè)置(3,B)的值是nan
·使用loc設(shè)置(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運行結(jié)果
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
3 0.298641 0.031346 0.678006 0.903489
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
8 0.049213 0.465223 0.941233 0.216512
9 0.361318 0.031319 0.304045 0.188268
A B C D
0 0.154288 0.133700 0.362685 0.679109
1 0.194450 0.251210 0.758416 0.557619
2 0.514803 0.467800 0.087176 0.829095
3 0.298641 NaN 0.678006 0.903489
4 0.514451 0.539105 0.664328 0.634057
5 0.353419 0.026643 0.165290 0.879319
6 0.067820 0.369086 0.115501 0.096294
7 0.083770 0.086927 0.022256 0.771043
8 0.049213 0.465223 0.941233 NaN
9 0.361318 0.031319 0.304045 0.188268
到此這篇關(guān)于Python Pandas 修改表格數(shù)據(jù)類型 DataFrame 列的順序案例的文章就介紹到這了,更多相關(guān)Python Pandas 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用matplotlib庫實現(xiàn)圖形局部數(shù)據(jù)放大顯示的實踐
本文主要介紹了使用matplotlib庫實現(xiàn)圖形局部數(shù)據(jù)放大顯示的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
python調(diào)用騰訊云實名認證接口辨別身份證真假
這篇文章主要為大家介紹了python辨別身份真假之騰訊云身份證實名認證接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Windows8下安裝Python的BeautifulSoup
這篇文章主要介紹了Windows8下安裝Python的BeautifulSoup,本文著重講解安裝中出現(xiàn)的錯誤和解決方法,需要的朋友可以參考下2015-01-01

