您好,登錄后才能下訂單哦!
這篇文章主要介紹“pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引如何實(shí)現(xiàn)”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引如何實(shí)現(xiàn)”文章能幫助大家解決問題。
在使用pandas時,由于隔行讀取刪除了部分?jǐn)?shù)據(jù),導(dǎo)致刪除數(shù)據(jù)后的索引不連續(xù):
在綁定pyqt的tableview時需進(jìn)行格式化,結(jié)果出現(xiàn)報錯:
主要原因是索引值不連續(xù),所以無法格式化,需對刪除數(shù)據(jù)后的數(shù)據(jù)集進(jìn)行重新索引,在格式化數(shù)據(jù)集之前加一句代碼:
new_biao = biao.reset_index(drop=True)
順利通過了編碼并顯示到tableview,問題解決。
class PandasModel(QtCore.QAbstractTableModel): """ Class to populate a table view with a pandas dataframe """ def __init__(self, data, parent=None): QtCore.QAbstractTableModel.__init__(self, parent) self._data = data def rowCount(self, parent=None): return len(self._data.values) def columnCount(self, parent=None): return self._data.columns.size def data(self, index, role=QtCore.Qt.DisplayRole): if index.isValid(): if role == QtCore.Qt.DisplayRole: return str(self._data.values[index.row()][index.column()]) return None def headerData(self, col, orientation, role): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: return self._data.columns[col] return None
使用:
model = PandasModel(your_pandas_data_frame) your_tableview.setModel(model)
很多情況下,我們的數(shù)據(jù)源是 CSV 文件。假設(shè)有一個名為的文件data.csv,包含以下數(shù)據(jù)。
date,temperature,humidity 07/01/21,95,50 07/02/21,94,55 07/03/21,94,56
默認(rèn)情況下,pandas將會創(chuàng)建一個從0開始的索引行,如下:
>>> pd.read_csv("data.csv", parse_dates=["date"]) date temperature humidity 0 2021-07-01 95 50 1 2021-07-02 94 55 2 2021-07-03 94 56
但是,我們可以在導(dǎo)入過程中通過將index_col參數(shù)設(shè)置為某一列可以直接指定索引列。
>>> pd.read_csv("data.csv", parse_dates=["date"], index_col="date") temperature humidity date 2021-07-01 95 50 2021-07-02 94 55 2021-07-03 94 56
當(dāng)然,如果已經(jīng)讀取數(shù)據(jù)或做完一些數(shù)據(jù)處理步驟后,我們可以通過set_index手動設(shè)置索引。
>>> df = pd.read_csv("data.csv", parse_dates=["date"]) >>> df.set_index("date") temperature humidity date 2021-07-01 95 50 2021-07-02 94 55 2021-07-03 94 56
這里有兩點(diǎn)需要注意下。
1.set_index方法默認(rèn)將創(chuàng)建一個新的 DataFrame。如果要就地更改df的索引,需要設(shè)置inplace=True。
df.set_index(“date”, inplace=True)
2.如果要保留將要被設(shè)置為索引的列,可以設(shè)置drop=False。
df.set_index(“date”, drop=False)
在處理 DataFrame 時,某些操作(例如刪除行、索引選擇等)將會生成原始索引的子集,這樣默認(rèn)的數(shù)字索引排序就亂了。如要重新生成連續(xù)索引,可以使用reset_index方法。
>>> df0 = pd.DataFrame(np.random.rand(5, 3), columns=list("ABC")) >>> df0 A B C 0 0.548012 0.288583 0.734276 1 0.342895 0.207917 0.995485 2 0.378794 0.160913 0.971951 3 0.039738 0.008414 0.226510 4 0.581093 0.750331 0.133022 >>> df1 = df0[df0.index % 2 == 0] >>> df1 A B C 0 0.548012 0.288583 0.734276 2 0.378794 0.160913 0.971951 4 0.581093 0.750331 0.133022 >>> df1.reset_index(drop=True) A B C 0 0.548012 0.288583 0.734276 1 0.378794 0.160913 0.971951 2 0.581093 0.750331 0.133022
通常,我們是不需要保留舊索引的,因此可將drop參數(shù)設(shè)置為True。同樣,如果要就地重置索引,可設(shè)置inplace參數(shù)為True,否則將創(chuàng)建一個新的 DataFrame。
groupby分組方法是經(jīng)常用的。比如下面通過添加一個分組列team來進(jìn)行分組。
>>> df0["team"] = ["X", "X", "Y", "Y", "Y"] >>> df0 A B C team 0 0.548012 0.288583 0.734276 X 1 0.342895 0.207917 0.995485 X 2 0.378794 0.160913 0.971951 Y 3 0.039738 0.008414 0.226510 Y 4 0.581093 0.750331 0.133022 Y >>> df0.groupby("team").mean() A B C team X 0.445453 0.248250 0.864881 Y 0.333208 0.306553 0.443828
默認(rèn)情況下,分組會將分組列編程index索引。但是很多情況下,我們不希望分組列變成索引,因?yàn)榭赡苡行┯嬎慊蛘吲袛噙壿嬤€是需要用到該列的。因此,我們需要設(shè)置一下讓分組列不成為索引,同時也能完成分組的功能。
有兩種方法可以完成所需的操作,第一種是用reset_index,第二種是在groupby方法里設(shè)置as_index=False。個人更喜歡第二種方法,它只涉及兩個步驟,更簡潔。
>>> df0.groupby("team").mean().reset_index() team A B C 0 X 0.445453 0.248250 0.864881 1 Y 0.333208 0.306553 0.443828 >>> df0.groupby("team", as_index=False).mean() team A B C 0 X 0.445453 0.248250 0.864881 1 Y 0.333208 0.306553 0.443828
當(dāng)用sort_value排序方法時也會遇到這個問題,因?yàn)槟J(rèn)情況下,索引index跟著排序順序而變動,所以是亂雪。如果我們希望索引不跟著排序變動,同樣需要在sort_values方法中設(shè)置一下參數(shù)ignore_index即可。
>>> df0.sort_values("A") A B C team 3 0.039738 0.008414 0.226510 Y 1 0.342895 0.207917 0.995485 X 2 0.378794 0.160913 0.971951 Y 0 0.548012 0.288583 0.734276 X 4 0.581093 0.750331 0.133022 Y >>> df0.sort_values("A", ignore_index=True) A B C team 0 0.039738 0.008414 0.226510 Y 1 0.342895 0.207917 0.995485 X 2 0.378794 0.160913 0.971951 Y 3 0.548012 0.288583 0.734276 X 4 0.581093 0.750331 0.133022 Y
刪除重復(fù)項和排序一樣,默認(rèn)執(zhí)行后也會打亂排序順序。同理,可以在drop_duplicates方法中設(shè)置ignore_index參數(shù)True即可。
>>> df0 A B C team 0 0.548012 0.288583 0.734276 X 1 0.342895 0.207917 0.995485 X 2 0.378794 0.160913 0.971951 Y 3 0.039738 0.008414 0.226510 Y 4 0.581093 0.750331 0.133022 Y >>> df0.drop_duplicates("team", ignore_index=True) A B C team 0 0.548012 0.288583 0.734276 X 1 0.378794 0.160913 0.971951 Y
當(dāng)我們有了一個 DataFrame 時,想要使用不同的數(shù)據(jù)源或單獨(dú)的操作來分配索引。在這種情況下,可以直接將索引分配給現(xiàn)有的 df.index。
>>> better_index = ["X1", "X2", "Y1", "Y2", "Y3"] >>> df0.index = better_index >>> df0 A B C team X1 0.548012 0.288583 0.734276 X X2 0.342895 0.207917 0.995485 X Y1 0.378794 0.160913 0.971951 Y Y2 0.039738 0.008414 0.226510 Y Y3 0.581093 0.750331 0.133022 Y
數(shù)據(jù)導(dǎo)出到 CSV 文件時,默認(rèn) DataFrame 具有從 0 開始的索引。如果我們不想在導(dǎo)出的 CSV 文件中包含它,可以在to_csv方法中設(shè)置index參數(shù)。
>>> df0.to_csv("exported_file.csv", index=False)
如下所示,導(dǎo)出的 CSV 文件中,索引列未包含在文件中。
其實(shí),很多方法中都有關(guān)于索引的設(shè)置,只不過大家一般比較關(guān)心數(shù)據(jù),而經(jīng)常忽略了索引,才導(dǎo)致繼續(xù)運(yùn)行時可能會報錯。以上幾個高頻的操作都是有索引設(shè)置的,建議大家平時用的時候養(yǎng)成設(shè)置索引的習(xí)慣,這樣會節(jié)省不少時間。
關(guān)于“pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引如何實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。