Merge Rows Together Who Have The Same Value In A Column
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 wou
Solution 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]]
]
Post a Comment for "Merge Rows Together Who Have The Same Value In A Column"