是否可以比较数据框中的部分列?我有下面的Dataframe示例,其中保存了4种语言(en、de、nl、ua),每种语言都应该具有相同的键/相同数量的键,但具有不同的值(将static列保留在那里以供完成,因为我确实有一个静态列,其值始终保持不变)。你知道吗
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
我需要检查每种语言与英语(这里是“en”)相比缺少了哪些键和多少键,因此类似这样的内容将是所需的输出:
│ Lang │ Static │ # Missing │ Keys │
│ de │ x │ 0 │ │
│ nl │ x │ 1 │ key_3 │
│ ua │ x │ 2 │ key_2, key_3 │
这是我目前的进展:
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?
这是df_summ的输出:
keys values
count count
static language
x de 3 3
en 3 3
nl 2 2
ua 1 1
在这一点上我不知道如何继续。我很感激你的帮助/提示。你知道吗
另外,这是在python3.5上实现的。你知道吗
编辑:
编辑:
我尝试更改数据:
输出为:
问题是对于
de
对于y
静态,在en语言中有更多的键。你知道吗因为您在问题中添加了
R
标记,下面介绍如何使用tidyr
和dplyr
:数据
相关问题 更多 >
编程相关推荐