Merge rows together who have the same value in a column

大兔子大兔子 提交于 2021-02-10 17:44:30

问题


I have a CSV file like this (which parsed by using the pandas by using read_csv):

Filename f1 f2 f3
1.jpg     1 0.2 0.3
1.jpg     0 0.8 0.7       
2.jpg     1 0.3  0.2    

How would I use this dataset and change it into a numpy array which will look like this:

[
   [[1,0.2,0.3],[0,0.8.0.7]],
   [[1,0.3,0.2]]
]

回答1:


You can create nested lists by GroupBy.apply with lambda function, DataFrame.set_index is for avoid convert column Filename to lists:

df = pd.read_csv(file)

L = (df.set_index('Filename')
       .groupby('Filename')
       .apply(lambda x: x.to_numpy().tolist())
       .tolist())
print (L)

Or:

df = pd.read_csv(file, index_col=0)

L = (df.groupby('Filename')
       .apply(lambda x: x.to_numpy().tolist())
       .tolist())
print (L)

[
 [[1.0, 0.2, 0.3], [0.0, 0.8, 0.7]], 
 [[1.0, 0.3, 0.2]]
]


来源:https://stackoverflow.com/questions/65840573/merge-rows-together-who-have-the-same-value-in-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!