【问题标题】:Get sliced column values获取切片列值
【发布时间】:2017-01-27 19:54:51
【问题描述】:

我正在使用 iris.scale 数据集进行分析。但是在处理中我如何在读取数据文件后获得切片列值,

df = pd.read_csv("../Data/iris.scale.csv", sep=' ', header=None, names=['class','S.lenght','S.width','P.lenght','P.width'])
print(df.head(3))

   class     S.lenght       S.width     P.lenght      P.width
     1        1:-0.555556    2:0.25      3:-0.864407     4:-0.916667
     1        1:-0.666667    2:-0.166667 3:-0.864407     4:-0.916667
     1        1:-0.833333    2:-0.08333  3:-0.830508     4:-0.916667

但是如何得到这些分片的列,像这个没有任何特征引用,所以可以处理

class     S.lenght       S.width     P.lenght      P.width
     1        -0.555556    0.25       -0.864407     -0.916667
     1        -0.666667   -0.166667   -0.864407     -0.916667
     1        -0.833333   -0.08333    -0.830508     -0.916667

【问题讨论】:

标签: python pandas machine-learning


【解决方案1】:

pandas

  • filter 专注于正确的列
  • stack + str.split + unstack
  • update

代码

df.update(
    df.filter(regex='S|P').stack().str.split(':').str[1].astype(float).unstack())
df

   class  S.lenght   S.width  P.lenght   P.width
0      1 -0.555556      0.25 -0.864407 -0.916667
1      1 -0.666667 -0.166667 -0.864407 -0.916667
2      1 -0.833333  -0.08333 -0.830508 -0.916667

numpy

  • split 一次整个数组
  • 构造新数组
  • 切片和分配

代码

s = np.core.defchararray.split(df.values[:, 1:].astype(str), ':').tolist()
df.iloc[:, 1:] = np.array(s)[:, :, 1].astype(float)

   class  S.lenght   S.width  P.lenght   P.width
0      1 -0.555556      0.25 -0.864407 -0.916667
1      1 -0.666667 -0.166667 -0.864407 -0.916667
2      1 -0.833333  -0.08333 -0.830508 -0.916667

【讨论】:

    【解决方案2】:

    您可以使用set_index 创建DataFrame: 的值仅由split 提取,最后转换输出到float

    df=df.set_index('class').apply(lambda x: x.str.split(':').str[1]).astype(float).reset_index()
    print (df)
       class  S.lenght   S.width  P.lenght   P.width
    0      1 -0.555556  0.250000 -0.864407 -0.916667
    1      1 -0.666667 -0.166667 -0.864407 -0.916667
    2      1 -0.833333 -0.083330 -0.830508 -0.916667
    

    str.extract 的另一个解决方案:

    df = df.set_index('class').apply(lambda x: x.str.extract(':(.*)', expand=False)).astype(float).reset_index()
    print (df)
       class  S.lenght   S.width  P.lenght   P.width
    0      1 -0.555556  0.250000 -0.864407 -0.916667
    1      1 -0.666667 -0.166667 -0.864407 -0.916667
    2      1 -0.833333 -0.083330 -0.830508 -0.916667
    

    【讨论】:

      【解决方案3】:

      在将数据提供给 Pandas 之前对其进行预处理以去除多余的数据

      import re, io
      with open("../Data/iris.scale.csv") as f:
          data = f.read()
      
      p = r'[1-4]:'
      data = re.sub(p, '', data)
      

      然后,您可以将数据写入新文件,然后再将其提供给 Pandas,或者将其放入类似文件的对象中,然后再将其提供给 Pandas。

      #Python 2.7
      data = io.BytesIO(data)
      #Python 3x
      #data = io.StringIO(data)
      df = pd.read_csv(data, delim_whitespace = True, index_col = False, names=['class','S.lenght','S.width','P.lenght','P.width'])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2021-04-29
        相关资源
        最近更新 更多