- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个由列节点、组件和前导词组成的数据框。节点包含许多相同的值(按字母顺序排序),组件也包含许多相同的值,但被打乱了,前面的词可以是所有类型的词 - 但有些也相同。
我现在想做的是创建某种横截面/频率列表,显示链接到节点的组件和前面单词的频率。
假设这是我的 df:
node precedingWord comp
banana the lel
banana a lel
banana a lal
coconut some lal
coconut few lil
coconut the lel
我期待一个显示每个唯一节点的频率列表,以及在给定匹配条件的其他列中找到某个值的时间,例如
det1 = a
det2 = the
comp1 = lel
comp2 = lil
comp 3 = lal
预期输出:
node det1 det2 unspecified comp1 comp2 comp3
banana 2 1 0 2 0 1
coconut 0 1 0 1 1 1
我已经为一个变量做了这件事,但我不知道如何让 comp 列就位:
det1 = ["a"]
det2 = ["the"]
df.loc[df.preceding_word.isin(det1), "determiner"] = "det1"
df.loc[df.preceding_word.isin(det2), "determiner"] = "det2"
df.loc[df.preceding_word.isin(det1 + det2) == 0, "determiner"] = "unspecified"
# Create crosstab of the node and gender
freqDf = pd.crosstab(df.node, df.determiner)
我从 here 得到了这个答案.如果有人能解释 loc
的作用,那也会有很大帮助。
考虑到安迪的回答,我尝试了以下方法。请注意,“precedingWord”已替换为“gender”,它仅包含值 neuter、non_neuter、gender。
def frequency_list():
# Define content of gender classes
neuter = ["het"]
non_neuter = ["de"]
# Add `gender` column to df
df.loc[df.preceding_word.isin(neuter), "gender"] = "neuter"
df.loc[df.preceding_word.isin(non_neuter), "gender"] = "non_neuter"
df.loc[df.preceding_word.isin(neuter + non_neuter) == 0, "gender"] = "unspecified"
g = df.groupby("node")
# Create crosstab of the node, and gender and component
freqDf = pd.concat([g["component"].value_counts().unstack(1), g["gender"].value_counts().unstack(1)])
# Reset indices, starting from 1, not the default 0!
""" Crosstabs don't come with index, so we first set the index with
`reset_index` and then alter it. """
freqDf.reset_index(inplace=True)
freqDf.index = np.arange(1, len(freqDf) + 1)
freqDf.to_csv("dataset/py-frequencies.csv", sep="\t", encoding="utf-8")
输出接近我想要的,但不完全是:
component
) 的行,然后显示 gender
的相同节点。 0
。那么,我想要的是:
请注意,我正在寻找最有效的答案。我实际上正在处理负载和数据负载,因此每个循环的每一秒都很重要!
最佳答案
更新:这是一个 crosstab
:
In [11]: df1 = pd.crosstab(df['node'], df['precedingWord'])
In [12]: df1
Out[12]:
precedingWord a few some the
node
banana 2 0 0 1
coconut 0 1 1 1
In [13]: df2 = pd.crosstab(df['node'], df['comp'])
这显然是一种更清洁(并且对大数据更有效的算法)。
然后用 axis=1 的连接将它们粘合起来(即添加更多的列而不是添加更多的行)。
In [14]: pd.concat([df1, df2], axis=1, keys=['precedingWord', 'comp'])
Out[14]:
precedingWord comp
a few some the lal lel lil
node
banana 2 0 0 1 1 2 0
coconut 0 1 1 1 1 1 1
我可能会像这样保留它(作为一个 MultiIndex),如果你想让它变平只是不要传递键(尽管重复的单词可能有问题):
In [15]: pd.concat([df1, df2], axis=1)
Out[15]:
a few some the lal lel lil
node
banana 2 0 0 1 1 2 0
coconut 0 1 1 1 1 1 1
旁白:如果 concat 不要求在列名称存在时显式传入(作为键 kwarg),那就太好了...
您可以使用 value_counts
:
In [21]: g = df.groupby("node")
In [22]: g["comp"].value_counts()
Out[22]:
node comp
banana lel 2
lal 1
coconut lal 1
lel 1
lil 1
dtype: int64
In [23]: g["precedingWord"].value_counts()
Out[23]:
node precedingWord
banana a 2
the 1
coconut few 1
some 1
the 1
dtype: int64
将它放在一个框架中有点棘手:
In [24]: pd.concat([g["comp"].value_counts().unstack(1), g["precedingWord"].value_counts().unstack(1)])
Out[24]:
a few lal lel lil some the
node
banana NaN NaN 1 2 NaN NaN NaN
coconut NaN NaN 1 1 1 NaN NaN
banana 2 NaN NaN NaN NaN NaN 1
coconut NaN 1 NaN NaN NaN 1 1
In [25]: pd.concat([g["comp"].value_counts().unstack(1), g["precedingWord"].value_counts().unstack(1)]).fillna(0)
Out[25]:
a few lal lel lil some the
node
banana 0 0 1 2 0 0 0
coconut 0 0 1 1 1 0 0
banana 2 0 0 0 0 0 1
coconut 0 1 0 0 0 1 1
您可以在执行连接之前将列映射到 det1、det2 等,例如,如果您将映射作为字典。
In [31]: res = g["comp"].value_counts().unstack(1)
In [32]: res
Out[32]:
comp lal lel lil
node
banana 1 2 NaN
coconut 1 1 1
In [33]: res.columns = res.columns.map({"lal": "det1", "lel": "det2", "lil": "det3"}.get)
In [34]: res
Out[34]:
det1 det2 det3
node
banana 1 2 NaN
coconut 1 1 1
或者,您可以使用列表推导式(如果您没有字典或心中有特定的标签):
In [41]: res = g["comp"].value_counts().unstack(1)
In [42]: res.columns = ['det%s' % i for i, _ in enumerate(df.columns)]
关于python - 在 Python 中使用两个列变量将数据框转换为频率列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611776/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!