Pandas
Pandas (55)
알 수 없는 사용자
2022. 10. 14. 22:17
728x90
How to reshape a dataframe to the largest possible square after removing the negative values?
# Input
df = pd.DataFrame(np.random.randint(-20, 50, 100).reshape(10,-1))
print(df)
# Solution
# Step 1: remove negative values from arr
arr = df[df > 0].values.flatten()
arr_qualified = arr[~np.isnan(arr)]
# Step 2: find side-length of largest possible square
n = int(np.floor(arr_qualified.shape[0]**.5))
# Step 3: Take top n^2 items without changing positions
top_indexes = np.argsort(arr_qualified)[::-1]
output = np.take(arr_qualified, sorted(top_indexes[:n**2])).reshape(n, -1)
print(output)
# output
0 1 2 3 4 5 6 7 8 9
0 25 -13 17 16 0 6 22 44 10 -19
1 47 4 -1 29 -13 12 41 -13 49 42
2 20 -20 9 16 -17 -1 37 39 41 37
3 27 44 -5 5 3 -12 0 -13 23 45
4 8 27 -8 -3 48 -16 -5 40 16 10
5 12 12 41 -12 3 -17 -3 27 -15 -1
6 -9 -3 41 -13 1 0 28 33 -2 18
7 18 -14 35 5 4 14 4 44 14 34
8 1 24 26 28 -10 17 -14 14 38 17
9 13 12 5 9 -16 -7 12 -18 1 24
[[ 25. 17. 16. 6. 22. 44. 10. 47.]
[ 4. 29. 12. 41. 49. 42. 20. 9.]
[ 16. 37. 39. 41. 37. 27. 44. 5.]
[ 3. 23. 45. 8. 27. 48. 40. 16.]
[ 10. 12. 12. 41. 3. 27. 41. 28.]
[ 33. 18. 18. 35. 5. 4. 14. 4.]
[ 44. 14. 34. 24. 26. 28. 17. 14.]
[ 38. 17. 13. 12. 5. 9. 12. 24.]]