[英]Splitting sentences in a column and then appending in a data frame in python
I have a data frame in python df. 我在python df中有一个数据框。
Its structure is as follows :- 其结构如下:
Sentences | Value
This is my house | 0
My house is good | 2
. 。 .
。 .
。 .
。
Now what I want it to split the column sentence to words and then have a pandas data frame to append these words with their original sentence value in front of them. 现在我想要将列句子拆分为单词,然后有一个pandas数据框将这些单词附加在其前面的原始句子值。
The output should be as follows:- 输出应如下所示:
Words | Value
This | 0
is | 0
my | 0
house | 0
My | 2
house | 2
is | 2
good | 2
. 。 .
。 .
。
I have used a function to split the sentences. 我使用了一个拆分句子的功能。
def makeTermsFrom(msg):
return [m for m in msg.lower().split() if m]
a = readMessagesFromFile("./data/a_labelled.txt") #Returns a df
b = makeTermsFrom(a['Sentences'].iloc[0]) #Splits the sentences
but I was not able to add the words with their values in a df. 但是我无法在df中添加带有其值的单词。
Use the DataFrame.itertuples()
method: 使用
DataFrame.itertuples()
方法:
import pandas as pd
df = pd.DataFrame(
[['John Lennon', 10], ['George Harrison', 6]],
columns=['beatle', 'songs']
)
longform = pd.DataFrame(columns=['word', 'num'])
for idx, name, songs in df.itertuples():
name_words = (i.lower() for i in name.split())
longform = longform.append(
[{'word': nw, 'num': songs} for nw in name_words],
ignore_index=True
)
print(longform.head())
# word num
# 0 john 10
# 1 lennon 10
# 2 george 6
# 3 harrison 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.