回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>是否可以比较数据框中的部分列?我有下面的Dataframe示例,其中保存了4种语言(en、de、nl、ua),每种语言都应该具有相同的键/相同数量的键,但具有不同的值(将static列保留在那里以供完成,因为我确实有一个静态列,其值始终保持不变)。你知道吗</p>
<pre><code>static │ langs │ keys │ values
x │ en │ key_1 │ value_en_1
x │ en │ key_2 │ value_en_2
x │ en │ key_3 │ value_en_3
x │ de │ key_1 │ value_de_1
x │ de │ key_2 │ value_de_2
x │ de │ key_3 │ value_de_3
x │ nl │ key_1 │ value_nl_1
x │ nl │ key_2 │ value_nl_2
x │ ua │ key_1 │ value_ua_1
</code></pre>
<p>我需要检查每种语言与英语(这里是“en”)相比缺少了哪些键和多少键,因此类似这样的内容将是所需的输出:</p>
<pre><code>│ Lang │ Static │ # Missing │ Keys │
│ de │ x │ 0 │ │
│ nl │ x │ 1 │ key_3 │
│ ua │ x │ 2 │ key_2, key_3 │
</code></pre>
<p>这是我目前的进展:</p>
<pre><code>import pandas as pd
# this is read from a csv, but I'll leave it as list of lists for simplicity
rows = [
['x', 'en', 'key_1', 'value_en_1'],
['x', 'en', 'key_2', 'value_en_2'],
['x', 'en', 'key_3', 'value_en_3'],
['x', 'de', 'key_1', 'value_de_1'],
['x', 'de', 'key_2', 'value_de_2'],
['x', 'de', 'key_3', 'value_de_3'],
['x', 'nl', 'key_1', 'value_nl_1'],
['x', 'nl', 'key_2', 'value_nl_2'],
['x', 'ua', 'key_1', 'value_en_1']
]
# create DataFrame out of rows of data
df = pd.DataFrame(rows, columns=["static", "language", "keys", "values"])
# print out DataFrame
print("Dataframe: ", df)
# first group by language and the static column
df_grp = df.groupby(["static", "language"])
# try to sum the number of keys and values per each language
df_summ = df_grp.agg(["count"])
# print out the sums
print()
print(df_summ)
# how to compare?
# how to get the keys?
</code></pre>
<p>这是df_summ的输出:</p>
<pre><code> keys values
count count
static language
x de 3 3
en 3 3
nl 2 2
ua 1 1
</code></pre>
<p>在这一点上我不知道如何继续。我很感激你的帮助/提示。你知道吗</p>
<p>另外,这是在python3.5上实现的。你知道吗</p>