简体   繁体   English

Python Pandas:将一列的值检查到另一列 dataframe

[英]Python Pandas: checking value of one column into column of another dataframe

I have two data frames which looks like following:我有两个数据框,如下所示:

df:东风:

         Review Text                                        Noun                                             Thumbups   Rating
    I've been using this app for over a month. It ...   [app, month, job, track, ATV, replay, animatio...         2.0   4
    Would be nice to be able to import files from ...   [My, Tracks, app, phone, Google, Drive, import...         6.0   5
    When screen off it shows a straight line. Not ...   [screen, line, route]                                     1.0   3
    No Offline Maps! It used to have offline maps ...   [Offline, Maps, menu, option, video, exchange,...         20.0  1
    Great application. Designed with very well tho...   [application, application]                                20.0  5
    Great App. Nice and simple but accurate. Wish ...   [Great, App, Nice, Exported]                                0.0 5
    Does just what it says. Had a couple of questi...   [couple, service]                                         0.0   5
    Save For Offline - This does not work. The rou...   [Save, Offline, route, filesystem]                       12.0   1
    Since latest update app will not run. Subscrip...   [update, app, Subscription, March, application]           9.0   5
    Great app. Love it! And all the things it does...   [Great, app, Thank, work]                                1.0    5
    I have paid for subscription but keeps telling...   [subscription, trial, period]                            0.0    2
    Error: The route cannot be save for no locatio...   [Error, route, i, GPS]                                   0.0    2

df1: df1:

Noun    Thumb_count
accuracy    1.0
almost      1.0
animation   2.0
antarctica  1.0
app         25.0
application 29.0
apps        1.0
atv         2.0
august      3.0
battery     1.0

I want to check if the value of column 'Noun' of df1 present in 'Noun' column of df, then create a new column in df1 with name 'average' and take the average of 'Rating' column of df rows where the Noun value present.我想检查 df1 的“名词”列的值是否存在于 df 的“名词”列中,然后在 df1 中创建一个名为“平均”的新列,并取 df 行的“评级”列的平均值,其中名词存在的价值。

I started with comparing two columns of dataframe by using following code:我开始使用以下代码比较 dataframe 的两列:

df['Noun'].isin(set(df1['Noun']))

However, I got TypeError and System Error: Following are the error:但是,我得到了 TypeError 和 System Error: 以下是错误:

TypeError: unhashable type: 'list'
SystemError: <built-in method view of numpy.ndarray object at 0x7ff6313e3df0> returned a result with an error set

Could anyone help me where am I making the mistake?谁能帮助我我在哪里犯了错误?

A sample output would have been very useful.样品 output 将非常有用。 In its absence, my attempt;在它缺席的情况下,我的尝试;

df.Noun=df.Noun.str.strip('[]')#Strip corner brackets
df.Noun=df.Noun.str.split(",")#Make list again.
df=df.explode('Noun')#Get each item in df.Noun 
df[df.Noun.str.contains(('|').join(df1.Noun.values.tolist()))]#Check membership
df.groupby('Noun')['Rating'].mean()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Pandas 数据框中检查一列并返回另一列 - Checking one column and returning another column, in a Pandas Dataframe 如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值? - How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas? Python Pandas检查值是否从一个DataFrame到另一个DataFrame - Python Pandas checking for a value if it exists from one DataFrame to another DataFrame Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 检查一个 dataframe 列是否是另一列的子集 - Checking if one dataframe column is a subset of another column 用pandas DataFrame中另一列的值填充一列 - Fill one column with value of another column in pandas DataFrame Python - 如何根据另一列中的值更改 pandas dataframe 的一列中的值组? - Python - How to change groups of values in one column of pandas dataframe depending on a value in another column? Python Pandas Dataframe 如何根据另一列的长度重复一列中的值 - Python Pandas Dataframe How to repeat a value in one column based on length of another column 使用 dataframe 列将缺失值添加到另一列 python pandas - Add the missing value from one dataframe column to another column using python pandas Python/Pandas:在一个 dataframe 中搜索日期,并在另一个 dataframe 的列中返回具有匹配日期的值 - Python/Pandas: Search for date in one dataframe and return value in column of another dataframe with matching date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM