Pandas : DataFrame的重组
Pandas : Reorganization of a DataFrame
我正在寻找清理以下数据的方法:
我想输出这样的东西:
第一列是标记化的词,另一列是它们的关联标签。
Pandas 和 NLTK 是否有特定的策略来一次获得这种类型的输出?
提前感谢您的帮助或建议
鉴于第一个 table,这只是 拆分第一列并重复第二列的问题:
import pandas as pd
data = [['foo bar', 'O'], ['George B', 'PERSON'], ['President', 'TITLE']]
df1 = pd.DataFrame(data, columns=['col1', 'col2'])
print(df1)
df2 = pd.concat([pd.Series(row['col2'], row['col1'].split(' '))
for _, row in df1.iterrows()]).reset_index()
df2 = df2.rename(columns={'index': 'col1', 0: 'col2'})
print(df2)
输出:
col1 col2
0 foo bar O
1 George B PERSON
2 President TITLE
col1 col2
0 foo O
1 bar O
2 George PERSON
3 B PERSON
4 President TITLE
至于第1列的拆分,你想看看支持正则表达式的拆分方法,它应该允许你处理各种语言分隔符:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html
如果第一个 table 未给出,则无法在 1 中使用 pandas 执行此操作,因为 pandas 尚未构建-NLP 能力。
我正在寻找清理以下数据的方法:
我想输出这样的东西:
第一列是标记化的词,另一列是它们的关联标签。
Pandas 和 NLTK 是否有特定的策略来一次获得这种类型的输出?
提前感谢您的帮助或建议
鉴于第一个 table,这只是 拆分第一列并重复第二列的问题:
import pandas as pd
data = [['foo bar', 'O'], ['George B', 'PERSON'], ['President', 'TITLE']]
df1 = pd.DataFrame(data, columns=['col1', 'col2'])
print(df1)
df2 = pd.concat([pd.Series(row['col2'], row['col1'].split(' '))
for _, row in df1.iterrows()]).reset_index()
df2 = df2.rename(columns={'index': 'col1', 0: 'col2'})
print(df2)
输出:
col1 col2
0 foo bar O
1 George B PERSON
2 President TITLE
col1 col2
0 foo O
1 bar O
2 George PERSON
3 B PERSON
4 President TITLE
至于第1列的拆分,你想看看支持正则表达式的拆分方法,它应该允许你处理各种语言分隔符: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html
如果第一个 table 未给出,则无法在 1 中使用 pandas 执行此操作,因为 pandas 尚未构建-NLP 能力。