久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何通過(guò) pandas 或火花數(shù)據(jù)框刪除所有行中具有相

How to drop columns which have same values in all rows via pandas or spark dataframe?(如何通過(guò) pandas 或火花數(shù)據(jù)框刪除所有行中具有相同值的列?)
本文介紹了如何通過(guò) pandas 或火花數(shù)據(jù)框刪除所有行中具有相同值的列?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

假設(shè)我有類似以下的數(shù)據(jù):

Suppose I've data similar to following:

  index id   name  value  value2  value3  data1  val5
    0  345  name1    1      99      23     3      66
    1   12  name2    1      99      23     2      66
    5    2  name6    1      99      23     7      66

我們?nèi)绾卧谝粋€(gè)命令中刪除所有行具有相同值的所有列,例如 (value, value2, value3)還是使用 python 的幾個(gè)命令?

How can we drop all those columns like (value, value2, value3) where all rows have the same values, in one command or couple of commands using python?

假設(shè)我們有許多列類似于 valuevalue2value3...value200.

Consider we have many columns similar to value, value2, value3...value200.

輸出:

   index    id  name   data1
       0   345  name1    3
       1    12  name2    2
       5     2  name6    7

推薦答案

我們可以做的是使用 nunique 計(jì)算數(shù)據(jù)框每一列中唯一值的個(gè)數(shù),并丟棄只有一個(gè)唯一值:

What we can do is use nunique to calculate the number of unique values in each column of the dataframe, and drop the columns which only have a single unique value:

In [285]:
nunique = df.nunique()
cols_to_drop = nunique[nunique == 1].index
df.drop(cols_to_drop, axis=1)

Out[285]:
   index   id   name  data1
0      0  345  name1      3
1      1   12  name2      2
2      5    2  name6      7

另一種方法是只 diff 數(shù)字列,獲取 abs 值和 sums 它們:

Another way is to just diff the numeric columns, take abs values and sums them:

In [298]:
cols = df.select_dtypes([np.number]).columns
diff = df[cols].diff().abs().sum()
df.drop(diff[diff== 0].index, axis=1)
?
Out[298]:
   index   id   name  data1
0      0  345  name1      3
1      1   12  name2      2
2      5    2  name6      7

另一種方法是使用具有相同值的列的標(biāo)準(zhǔn)差為零的屬性:

Another approach is to use the property that the standard deviation will be zero for a column with the same value:

In [300]:
cols = df.select_dtypes([np.number]).columns
std = df[cols].std()
cols_to_drop = std[std==0].index
df.drop(cols_to_drop, axis=1)

Out[300]:
   index   id   name  data1
0      0  345  name1      3
1      1   12  name2      2
2      5    2  name6      7

其實(shí)以上都可以單行完成:

Actually the above can be done in a one-liner:

In [306]:
df.drop(df.std()[(df.std() == 0)].index, axis=1)

Out[306]:
   index   id   name  data1
0      0  345  name1      3
1      1   12  name2      2
2      5    2  name6      7

這篇關(guān)于如何通過(guò) pandas 或火花數(shù)據(jù)框刪除所有行中具有相同值的列?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美日韩精品影院 | 中文字幕日韩欧美一区二区三区 | 精品国产一区二区在线 | 亚洲视频中文字幕 | 欧美亚洲视频在线观看 | www.久久久久久久久久久久 | 国产一区二区三区久久 | av在线成人 | 亚州中文字幕 | 成人免费淫片aa视频免费 | 久久99精品久久久久久狂牛 | 欧美日韩国产一区二区 | 精品网 | 国产小视频在线 | 欧洲一区二区三区 | 成人三级视频在线观看 | 国产精品高潮呻吟久久av野狼 | 99reav| 一级黄片一级毛片 | 精品国产伦一区二区三区观看体验 | 国产激情片在线观看 | 一区二区日本 | 亚洲乱码一区二区 | 久久国内精品 | 毛片.com | 美女黄网| 国产精品一区二区av | 国产精品日产欧美久久久久 | 看av网址| 一区二区三区不卡视频 | 91精品福利 | 久久久一区二区三区四区 | 国产在线观看网站 | 亚洲精品久久久久久久久久久久久 | h片在线看 | 国产高清在线观看 | 午夜色婷婷| 成人小视频在线观看 | 欧美日本在线观看 | 国产95在线 | 久久r免费视频 |