如果单元格的值为 1,则使用列名转换数据集中的 0-1 值
Converting 0-1 values in dataset with the name of the column if the value of the cell is 1
我有一个 csv 数据集,元素的特征值为 0-1。我想迭代每个单元格并将值 1 替换为其列的名称。有超过 50 万行和 200 列,因为 table 是从我经常更新的另一个注释工具导出的,所以我想在 Python 中找到一种自动执行的方法。
This is not the table, but a sample test which I was using while trying to write a code 我尝试了一些,但没有成功。
如果您能与我分享您的知识,我将不胜感激。这将是一个巨大的帮助。我想要的最终结果是这样的类型:(abonojnë, token_pos_verb)。如果你知道我在 Excel 中不用 Python 的帮助就可以做到这一点的任何方法,那就更好了。
谢谢,
布里克纳
Text,Comment,Role,ParentID,doc_completeness,lemma,MultiWord_Expr,token,pos,punctuation,verb,noun,adjective
abonojnë,,,,,,,1,1,0,1,0,0
çokasin,,,,,,,1,1,0,1,0,1
gërgasin,,,,,,,1,1,0,1,0,0
godasin,,,,,,,1,1,0,1,0,0
përkasin,,,,,,,1,1,1,1,0,0
përdjegin,,,,,,,1,1,0,1,0,0
lakadredhin,,,,,,,1,1,0,1,1,0
përdredhin,,,,,,,1,1,0,1,0,0
spërdredhin,,,,,,,1,1,0,1,0,0
përmbledhin,,,,,,,1,1,0,1,0,0
shpërdredhin,,,,,,,1,1,0,1,0,0
arsejnë,,,,,,,1,1,0,1,1,0
çapëlejnë,,,,,,,1,1,0,1,0,0
使用pandas,这很简单:
# pip install pandas
import pandas as pd
# read data (here example with csv, but use "read_excel" for excel)
df = pd.read_csv('input.csv').set_index('Text')
# reshape and export
(df.mul(df.columns).where(df.eq(1))
.stack().rename('xxx')
.groupby(level=0).apply('_'.join)
).to_csv('output.csv') # here use "to_excel" for excel format
输出文件:
Text,xxx
abonojnë,token_pos_verb
arsejnë,token_pos_verb_noun
godasin,token_pos_verb
gërgasin,token_pos_verb
lakadredhin,token_pos_verb_noun
përdjegin,token_pos_verb
përdredhin,token_pos_verb
përkasin,token_pos_punctuation_verb
përmbledhin,token_pos_verb
shpërdredhin,token_pos_verb
spërdredhin,token_pos_verb
çapëlejnë,token_pos_verb
çokasin,token_pos_verb_adjective
更新给那些将来可能会觉得有用的人。感谢@mozway 帮助我。我的一个朋友建议使用 Excel 公式,因为 Pandas 和 gropuby 的解决方案消除了重复项。因为我需要所有的重复项,因为它是一个带注释的语料库,所以在每个上下文中都有重复的词是正常的,而不仅仅是第一次出现。
- 另一种选择是:
在 excel 文件上使用第二个 sheet,在第一个单元格中用 0-1 值写入公式 =IF(Sheet1!B2=1,Sheet2!B,"")
,然后将其拖动到所有其他单元格中。这保留了单词的所有出现。它很快,而且像魔术一样工作。
我希望这对其他想要将 0-1 数据集转换为特征名称而无需编码的人有所帮助。
我有一个 csv 数据集,元素的特征值为 0-1。我想迭代每个单元格并将值 1 替换为其列的名称。有超过 50 万行和 200 列,因为 table 是从我经常更新的另一个注释工具导出的,所以我想在 Python 中找到一种自动执行的方法。 This is not the table, but a sample test which I was using while trying to write a code 我尝试了一些,但没有成功。 如果您能与我分享您的知识,我将不胜感激。这将是一个巨大的帮助。我想要的最终结果是这样的类型:(abonojnë, token_pos_verb)。如果你知道我在 Excel 中不用 Python 的帮助就可以做到这一点的任何方法,那就更好了。 谢谢, 布里克纳
Text,Comment,Role,ParentID,doc_completeness,lemma,MultiWord_Expr,token,pos,punctuation,verb,noun,adjective
abonojnë,,,,,,,1,1,0,1,0,0
çokasin,,,,,,,1,1,0,1,0,1
gërgasin,,,,,,,1,1,0,1,0,0
godasin,,,,,,,1,1,0,1,0,0
përkasin,,,,,,,1,1,1,1,0,0
përdjegin,,,,,,,1,1,0,1,0,0
lakadredhin,,,,,,,1,1,0,1,1,0
përdredhin,,,,,,,1,1,0,1,0,0
spërdredhin,,,,,,,1,1,0,1,0,0
përmbledhin,,,,,,,1,1,0,1,0,0
shpërdredhin,,,,,,,1,1,0,1,0,0
arsejnë,,,,,,,1,1,0,1,1,0
çapëlejnë,,,,,,,1,1,0,1,0,0
使用pandas,这很简单:
# pip install pandas
import pandas as pd
# read data (here example with csv, but use "read_excel" for excel)
df = pd.read_csv('input.csv').set_index('Text')
# reshape and export
(df.mul(df.columns).where(df.eq(1))
.stack().rename('xxx')
.groupby(level=0).apply('_'.join)
).to_csv('output.csv') # here use "to_excel" for excel format
输出文件:
Text,xxx
abonojnë,token_pos_verb
arsejnë,token_pos_verb_noun
godasin,token_pos_verb
gërgasin,token_pos_verb
lakadredhin,token_pos_verb_noun
përdjegin,token_pos_verb
përdredhin,token_pos_verb
përkasin,token_pos_punctuation_verb
përmbledhin,token_pos_verb
shpërdredhin,token_pos_verb
spërdredhin,token_pos_verb
çapëlejnë,token_pos_verb
çokasin,token_pos_verb_adjective
更新给那些将来可能会觉得有用的人。感谢@mozway 帮助我。我的一个朋友建议使用 Excel 公式,因为 Pandas 和 gropuby 的解决方案消除了重复项。因为我需要所有的重复项,因为它是一个带注释的语料库,所以在每个上下文中都有重复的词是正常的,而不仅仅是第一次出现。
- 另一种选择是:
在 excel 文件上使用第二个 sheet,在第一个单元格中用 0-1 值写入公式 =IF(Sheet1!B2=1,Sheet2!B,"")
,然后将其拖动到所有其他单元格中。这保留了单词的所有出现。它很快,而且像魔术一样工作。
我希望这对其他想要将 0-1 数据集转换为特征名称而无需编码的人有所帮助。