假设我有一个 Dataframe df,我想将它分割成多个 Dataframe ,并将每个 Dataframe 存储在一个列表(list_of_dfs)中。
每个子 Dataframe 应仅包含行“结果”。当列“点”中的值为“P1”且列“X_Y”中的值为“X”时,一个子 Dataframe 开始。
我试着先找到每个“P1”的索引,然后使用“P1”的索引在列表解析中分割整个 Dataframe 。但是我收到了一个有两个空 Dataframe 的列表。有人能给我建议吗?谢谢!
import pandas as pd
df = pd.DataFrame(
{
"Step": (
"1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "Result", "Result", "Result", "Result", "Result",
"1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "Result", "Result", "Result", "Result", "Result"
),
"Point": (
"P1", "P2", "P2", "P3", "P3", "P1", "P2", "P2", "P3", "P3", "P1", "P2", "P2", "P3", "P3",
"P1", "P2", "P2", "P3", "P3", "P1", "P2", "P2", "P3", "P3", "P1", "P2", "P2", "P3", "P3",
),
"X_Y": (
"X", "X", "Y", "X", "Y", "X", "X", "Y", "X", "Y", "X", "X", "Y", "X", "Y",
"X", "X", "Y", "X", "Y", "X", "X", "Y", "X", "Y", "X", "X", "Y", "X", "Y",
),
"Value A": (
70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72,
70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72,
),
"Value B": (
70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72,
70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72, 70, 68, 66.75, 68.08, 66.72,
),
}
)
dff = df.loc[df["Step"] == "Result"]
value = "P1"
tuple_of_positions = list()
result = dff.isin([value])
seriesObj = result.any()
columnNames = list(seriesObj[seriesObj == True].index)
for col in columnNames:
rows = list(result[col][result[col] == True].index)
for row in rows:
tuple_of_positions.append((row, col))
length_of_one_df = (len(dff["Point"].unique().tolist()) * 2 ) - 1
list_of_dfs = [dff.iloc[x : x + length_of_one_df] for x in rows]
print(list_of_dfs)
1条答案
按热度按时间3htmauhk1#
得到
中间人是
一个一个二个一个一个一个三个一个一个一个一个一个四个一个