将行从一个数据帧添加到另一个 [英] adding row from one dataframe to another

查看:53
本文介绍了将行从一个数据帧添加到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个数据帧插入或添加到另一个数据帧.我正在浏览原始数据框,以在一列中查找某些单词.当我找到这些术语之一时,我想将该行添加到新的数据框中.

I am trying to insert or add from one dataframe to another dataframe. I am going through the original dataframe looking for certain words in one column. When I find one of these terms I want to add that row to a new dataframe.

我通过使用获得了该行.entry = df.loc[df['A'] == item]但是当尝试使用 .add、.insert、.update 或其他方法将此行添加到另一个数据帧时,我只会得到一个空的数据帧.

I get the row by using. entry = df.loc[df['A'] == item] But when trying to add this row to another dataframe using .add, .insert, .update or other methods i just get an empty dataframe.

我还尝试将列添加到字典并将其转换为数据框,但它会为整行写入数据,而不仅仅是列值.那么有没有办法从我现有的变量中将一个特定的行添加到新的数据框中?

I have also tried adding the column to a dictionary and turning that into a dataframe but it writes data for the entire row rather than just the column value. So is there a way to add one specific row to a new dataframe from my existing variable ?

推荐答案

那么条目是一个包含要添加的行的数据框?如果两个数据框的列名称相同,您可以使用 concat 函数简单地连接两个数据框

So the entry is a dataframe containing the rows you want to add? you can simply concatenate two dataframe using concat function if both have the same columns' name

import pandas as pd

entry = df.loc[df['A'] == item]
concat_df = pd.concat([new_df,entry])

pandas.concat 参考:

pandas.concat reference:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

这篇关于将行从一个数据帧添加到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆