在数据框中找到一个序列并将其替换为原始行 [英] Find a series in dataframe and replace it with original row
问题描述
我的数据框为 df
,但是某些 D4
s为 True
在我的自定义排序中引起了问题。临时地,我将这样的行存储在列表中,并有意地将那些 D4
的值转换为 False
并按我的自定义顺序进行排序。
I have below dataframe df
but some D4
s with True
was causing an issue in my custom ordering. Temporarily, I stored such rows in a list and turned those D4
values to False
intentionally and sorted with my custom ordering.
Index D1 D2 D3 D4 D5
0 8 5 0 False True
1 45 35 0 True False
2 35 10 1 False True
3 40 5 0 True False
4 12 10 5 False False
5 18 15 13 False True
6 25 15 5 True False
7 35 10 11 False True
8 95 50 0 False False
hacked_rows = []
def hack_d4(row):
if row['D3'] in [0, 1]:
row['D4'] = False
hacked_rows.append(row)
return row
df_hacked = df.apply(lambda x: hack_d4(x), axis=1)
ordered_df = order_df(df_hacked) # Returns same df with some rows in custom order.
因此,从技术上讲,简而言之,我必须还原到 ordered_df
以下借助列表 hacked_rows
恢复到原始的 df
。行顺序并不重要,只应将被黑客入侵的行替换回原始数据集中。
So, Technically, in short I have to revert below ordered_df
to the original df
with the help of list hacked_rows
. Row Order is not important, only hacked rows should be replaced back in the original dataset.
Index D1 D2 D3 D4 D5
0 0 8 5 0 False True
2 2 35 10 1 False True
3 3 40 5 0 False False
1 1 45 35 0 False False
5 5 18 15 13 False True
4 4 12 10 5 False False
7 7 35 10 11 False True
8 8 95 50 0 False False
6 6 25 15 5 True False
现在我已经完成自定义订购。现在,我想将 hacked_rows
恢复为列表中存在的原始数据框,但是不确定如何替换它们。
Now I am done with custom ordering. Now I want to revert hacked_rows
back to the original dataframe which are there on the list, but not sure how to replace them back.
我在下面的代码中尝试了一行,但没有运气,它抛出了 TypeError
:
I tried below code for one row, but no luck, its throwing TypeError
:
item = hacked_rows[0]
item = item.drop('D3')
ordered_df.loc[item] # But this line is throwing error.
注意-我可以,如果有人可以建议使用其他方法替换 True
临时值。
Note- I am okay if anyone can suggest a different approach to replace the True
values temporarily.
推荐答案
我认为错误是在创建数据框时
I think the error is when you create the data frame again.
hacked_rows = []
def hack_d4(row):
if row['D3'] in [0, 1]:
row['D4'] = False
hacked_rows.append(row)
return row
df = df.apply(lambda x: hack_d4(x), axis=1)
ordered_df = pd.DataFrame(df) # code update
df
Index D1 D2 D3 D4 D5
0 0 8 5 0 False True
1 1 45 35 0 False False
2 2 35 10 1 False True
3 3 40 5 0 False False
4 4 12 10 5 False False
5 5 18 15 13 False True
6 6 25 15 5 True False
7 7 35 10 11 False True
8 8 95 50 0 False False
更新:
我添加了c理解我想将 hacked_rows
转换为数据帧列表,并加上您给我的评论。
Update:
I added the code with the understanding that I wanted to convert the hacked_rows
to a list of data frames, with the comment you gave me.
new_df = pd.DataFrame(index=[], columns=[])
for i in hacked_rows:
new_df = pd.concat([new_df, pd.Series(i)], axis=1, ignore_index=True)
new_df.stack().unstack(level=1).T
Index D1 D2 D3 D4 D5
1 0 8 5 0 False True
2 1 45 35 0 False False
3 2 35 10 1 False True
4 3 40 5 0 False False
5 8 95 50 0 False False
这篇关于在数据框中找到一个序列并将其替换为原始行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!