Wie verkettet man n Inhaltsreihen, die aktuelle Zeile, in einem Rollfenster in Pandas?
Posted: 11 May 2025, 22:22
Ich möchte einen Datenrahmen, der < /p>
enthält, transformieren
In [1, 2, 3, []], [4, 5, 6, [1, 2, 3, 4, 5, 6]], [7, 8, 9, [4, 5, 6, 7, 8, 9], [10, 11, 12, 8, 8, 9, 9, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12. mit IS: < /p>
Gibt es in der Pandas -Funktion, die ein Fenster rollen, keine Erstellung>
enthält, transformieren
Code: Select all
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
In [1, 2, 3, []], [4, 5, 6, [1, 2, 3, 4, 5, 6]], [7, 8, 9, [4, 5, 6, 7, 8, 9], [10, 11, 12, 8, 8, 9, 9, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12. mit IS: < /p>
Code: Select all
import pandas as pd
import numpy as np
# Create the DataFrame
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]))
# Initialize an empty list to store the result
result = []
# Iterate over the rows in the DataFrame
for i in range(len(df)):
# If it's the first row, append the row with an empty list
if i == 0:
result.append(list(df.iloc[i]) + [[]])
# If it's not the first row, concatenate the current and previous row
else:
current_row = list(df.iloc[i])
previous_row = list(df.iloc[i-1])
concatenated_row = current_row + [previous_row + current_row]
result.append(concatenated_row)
# Print the result
print(result)