如何删除列pandas中的重复值?

时间:2018-12-18 03:19:16

标签: python pandas

这是我的数据框,我分成了新的列。

DocID       0   1    2   3   4    5

CAT123     CAT  1   12 123  123  123
DOG14567   DOG  1   14 145 1456 14567
BIRD32     BIRD 3   32  32   32   32

,我想删除重复的值,然后看到这样的结果

    DocID       0   1    2   3   4    5

    CAT123     CAT  1   12 123  Nan  Nan
    DOG14567   DOG  1   14 145 1456 14567
    BIRD32     BIRD 3   32  Nan Nan  Nan

我该怎么做,我只知道删除行或列。预先谢谢你

3 个答案:

答案 0 :(得分:4)

使用duplicated + mask

df = df.mask(df.apply(pd.Series.duplicated,1))
df
Out[8]: 
      DocID     0  1   2      3       4        5
0    CAT123   CAT  1  12  123.0     NaN      NaN
1  DOG14567   DOG  1  14  145.0  1456.0  14567.0
2    BIRD32  BIRD  3  32    NaN     NaN      NaN

答案 1 :(得分:1)

只有两行答案:

 new_df = df.apply(pd.Series.duplicated, axis=1)
 df.where(~new_df, np.nan)

答案 2 :(得分:0)

您可以选择<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="HelloWorld.StackExercise2Page"> <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness" iOS="0, 20, 0, 0"> </OnPlatform> </ContentPage.Padding> <StackLayout> <StackLayout Padding="10"> <Label Text="generic username" /> </StackLayout> <Image Source="http://lorempixel.com/1920/1080/nature/3/" /> <StackLayout Orientation="Horizontal" Spacing="20" Padding="10, 0"> <Button Text="Like" /> <Button Text="Comment" /> <Button Text="Share" /> </StackLayout> <StackLayout Padding="10"> <BoxView HeightRequest="1" Color="#f0f0f0" /> <Label Text="700 likes" FontAttributes="Bold" /> <Label TextColor="#444" Text="This is a shot." /> </StackLayout> </StackLayout> </ContentPage> 重复的列,在这些列上进行迭代

df[df.iloc[:,i + 1] - d.iloc[:,i] == 0]