如何复制熊猫行

时间:2020-01-27 05:31:10

标签: python pandas

我的数据如下:

  '#filename'   file_size   file_attributes  region_count   region_id    region_shape_attributes     region_attributes  digits  digit_length

  0030004489.jpg    25188   {}                          3            1  {"name":"rect","x":143,"y":86,"width":201,"height":38}  {"class_type":"Medium Box"} 33328   6
  0030004489.jpg    25188   {}                          3            0  {"name":"rect","x":143,"y":75,"width":200,"height":68}  {"class_type":"Big Box"}    33328   6
  0030004745.jpg    23632   {}                          3            2  {"name":"rect","x":140,"y":61,"width":174,"height":47}  {"class_type":"Small Box"}  7631    8
  0030004745.jpg    23632   {}                          3            1  {"name":"rect","x":84,"y":62,"width":268,"height":45}   {"class_type":"Medium Box"} 7631    8
  0030004745.jpg    23632   {}                          3            0  {"name":"rect","x":86,"y":37,"width":266,"height":91}   {"class_type":"Big Box"}    7631    8
  0030005942.jpg    24178   {}                          3            2  {"name":"rect","x":268,"y":66,"width":84,"height":35}   {"class_type":"Small Box"}  196     4
  0030005942.jpg    24178   {}                          3            1  {"name":"rect","x":189,"y":66,"width":176,"height":35}  {"class_type":"Medium Box"} 196     4
  0030005942.jpg    24178   {}                          3            0  {"name":"rect","x":191,"y":48,"width":175,"height":64}  {"class_type":"Big Box"}    196     4
  0030012268.jpg    25450   {}                          3            2  {"name":"rect","x":218,"y":46,"width":113,"height":35}  {"class_type":"Small Box"}  369     6
  0030012268.jpg    25450   {}                          3            1  {"name":"rect","x":173,"y":46,"width":174,"height":35}  {"class_type":"Medium Box"} 369     6
  0030012268.jpg    25450   {}                          3            0  {"name":"rect","x":174,"y":35,"width":173,"height":45}  {"class_type":"Big Box"}    369     6

如何复制带有类类型小框的行,并将追加A_更改为复制行的文件名?我试图复制这样的行:

    for f in file_name:
        df=df.append((df.loc[(df['#filename']) == (str(f).replace("A_",""))].iloc[0]*100))
        df.to_csv("test.csv")
        break

但是问题在于,它会将所有复制的行附加到一个单元格中,如下所示: enter image description here

2 个答案:

答案 0 :(得分:0)

尝试一下

df = #your Dtaframe

df2 = df[(df['digit_length']) == '{"class_type":"Small Box"}']
df2['#filename'] = df2['#filename'].apply(lambda x:"A_"+x)

replicated_df = pd.concat([df,df2])

答案 1 :(得分:0)

这是使用sample的一种方式:

n_rows = 100
for f in file_name:

    # sample data or replicate rows
    f=df[df['#filename'] == f].sample(n_rows, replace=True)
    f['#filename'] = 'A_' + f['#filename']

    # concat data back to the original df
    df = pd.concat([df, f], ignore_index=True)