[英]dataframe how to add a specific row when a string is found in a given column
lets say I have a data frame P and where ever there is an in col b,假设我有一个数据框 P 并且在 col b 中有一个数据框,
I want to repeat that row, so that now it looks like:我想重复那一行,现在它看起来像:
From:
a b c d
1 v 4 5
4 n 6 7
5 v 6 8
To:
a b c d
1 v 4 5
4 n 6 7
4 n 6 7
5 v 6 8
I am quite new to python and have not found a straight forward way to accomplish this.我对 python 很陌生,还没有找到一种直接的方法来实现这一点。 here is what i have already tried
这是我已经尝试过的
if P['b']=='v':
P.pd.concat(P.loc,ignore_index=True)
You generally want to avoid looping through the DataFrame whenever you can, so if you want to find all of those rows, using loc with a boolean index can help you find them in one sweep, then you can copy what you found into a separate DataFrame.您通常希望尽可能避免遍历 DataFrame,因此如果您想找到所有这些行,使用带有布尔索引的 loc 可以帮助您一次性找到它们,然后您可以将找到的内容复制到单独的 DataFrame 中. Then, just concatenate the two.
然后,只需将两者连接起来。
p_2 = P.loc[P['b']=='n'].copy(deep=True)
P = pd.concat([P,P2],ignore_index=True)
You can use DataFrame.append :您可以使用DataFrame.append :
In [1]: df
Out[1]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
In [2]: df.append(df.loc[df['b'] == 'n'])
Out[2]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
1 4 n 6 7
Note that it appended the row at the end of the DataFrame.请注意,它在 DataFrame 的末尾附加了行。 If you want to have it next to the row being duplicated, you can use sort_index :
如果你想把它放在被复制的行旁边,你可以使用sort_index :
In [3]: df
Out[3]:
a b c d
0 1 v 4 5
1 4 n 6 7
2 5 v 6 8
In [4]: df.append(df.loc[df['b'] == 'n']).sort_index()
Out[4]:
a b c d
0 1 v 4 5
1 4 n 6 7
1 4 n 6 7
2 5 v 6 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.