简体   繁体   English

数据框如何在给定列中找到字符串时添加特定行

[英]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.

相关问题 如何在特定列和行上替换 dataframe 中的字符串? - how to replace string in dataframe on specific column and row? 如何在给定特定列/行的情况下将 for 循环的结果存储到 dataframe? - How to store results of a for loop to a dataframe given a specific column/row? 当在给定行中找到字典值时,使用字典键作为行值的新DataFrame列 - New DataFrame column using the key of a dictionary as row value when one of it's values is found in a given row 如何在数据框的每一行的列中添加特定值 - How to add specific value in column in in every row in dataframe 检查特定列中缺少的行,然后将其添加到数据框中 - Check missing row in specific column and then add it to the dataframe 从特定列向数据帧添加一行 - Add a row to a dataFrame from a specific column 如何将字符串添加到pandas dataframe列系列中的每个偶数行? - How to add a string to every even row in a pandas dataframe column series? 如何在特定列中的每个字符串的末尾添加一个单词(熊猫数据框) - How to add a word to the end of each string in a specific column (pandas dataframe) 如何检查 dataframe 中列名称为 Name 的特定股票代码字符串,如果在 dataframe 中找到,则返回? - How do I check for a specific ticker string inside my dataframe with a column name of Name and return if it is found in the dataframe? 如何从包含给定字符串的行开始切片两列熊猫数据帧? - How do I slice a two column pandas dataframe starting with a row containing a given string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM