问题
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