交换PANDAS数据框中的元素

时间:2018-12-04 11:25:14

标签: python pandas dataframe

以下是表格:

library(ggpubr)
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)

ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line",                         # Add regression line
          conf.int = TRUE,                          # Add confidence interval
          color = "cyl", palette = "jco",           # Color by groups "cyl"
          shape = "cyl"                             # Change point shape by groups "cyl"
)+
  stat_cor(aes(color = cyl,
               label =paste(..rr.label.., ..p.label.., sep = "~`,`~")), # HOW TO CHANGE p.label to show stars???
           label.x = 3)           # Add correlation coefficient

这最初是一张Excel工作表,现已转换为数据框。我希望交换一些元素,以便A数字列只有9999574081。 因此,输出应类似于:

    A NUMBER    B NUMBER
    7042967611  9999574081
    12320       9999574081
    9999574081  9810256463
    9999574081  9716551924
    9716551924  9999574081  
    9999574081  8130945859

这是我使用的代码:

    A NUMBER    B NUMBER
    9999574081  7042967611  
    9999574081  12320       
    9999574081  9810256463
    9999574081  9716551924
    9999574081  9716551924  
    9999574081  8130945859

但是,我没有得到想要的结果。请帮帮我。谢谢:)

1 个答案:

答案 0 :(得分:1)

使用DataFrame.loc仅交换匹配布尔掩码的行,values对于避免对齐索引值是必需的:

m = df['A NUMBER'] != 9999574081

df.loc[m, ['A NUMBER','B NUMBER']] = df.loc[m, ['B NUMBER','A NUMBER']].values

使用numpy.where的另一种解决方案:

df['B NUMBER'] = np.where(df['A NUMBER'] != 9999574081, df['A NUMBER'], df['B NUMBER'])
df['A NUMBER'] = 9999574081

print (df)
     A NUMBER    B NUMBER
0  9999574081  7042967611
1  9999574081       12320
2  9999574081  9810256463
3  9999574081  9716551924
4  9999574081  9716551924
5  9999574081  8130945859