การนับรายการคำในรายการสตริงโดยใช้ Python

ดังนั้นฉันจึงมีดาต้าเฟรมแพนด้าพร้อมแถวของสตริงโทเค็นในคอลัมน์ชื่อเรื่องราว ฉันยังมีรายการคำในรายการชื่อ Selected_words ด้วย ฉันกำลังพยายามนับอินสแตนซ์ของ select_words ใด ๆ ในแต่ละแถวในเรื่องของคอลัมน์

รหัสที่ฉันใช้ก่อนหน้านี้ได้ผลคือ

CCwordsCount=df4.story.str.count('|'.join(selected_words))

ตอนนี้ให้ค่า NaN แก่ฉันทุกแถว

ด้านล่างนี้คือสองสามแถวแรกของเรื่องราวของคอลัมน์ใน df4 dataframe มีบทความ NYTimes มากกว่า 400 แถวเล็กน้อย

0      [it, was, a, curious, choice, for, the, good, ...
1      [when, he, was, a, yale, law, school, student,...
2      [video, bitcoin, has, real, world, investors, ...
3      [bitcoin, s, wild, ride, may, not, have, been,...
4      [amid, the, incense, cheap, art, and, herbal, ...
5      [san, francisco, eight, years, ago, ernie, all...

นี่คือรายการของ Selected_words

selected_words = ['accept', 'believe', 'trust', 'accepted', 'accepts', 'trusts', 'believes', \
                  'acceptance', 'trusted', 'trusting', 'accepting', 'believes', 'believing', 'believed',\
                 'normal', 'normalize', ' normalized', 'routine', 'belief', 'faith', 'confidence', 'adoption', \
                  'adopt', 'adopted', 'embrace', 'approve', 'approval', 'approved', 'approves']

ลิงก์ไปยังไฟล์ df4 .csv ของฉัน


person Jesse-Burton Nicholson    schedule 13.05.2020    source แหล่งที่มา
comment
แต่ละรายการเป็นรายการที่มีสตริงเหมือนใน ["it, was, a, curious, choice, for, the, good, ..."] หรือไม่   -  person DarrylG    schedule 13.05.2020
comment
ใช่ ฉันเชื่อว่าแต่ละรายการคือรายการคำศัพท์ ฉันใช้ .split เพื่อแยกประโยคออกเป็นคำ การนับต้องเชื่อมโยงกับแต่ละรายการ เนื่องจากฉันกำลังเชื่อมโยงการนับกับข้อมูลอื่นๆ ในวันเดียวกันกับเรื่องราว   -  person Jesse-Burton Nicholson    schedule 13.05.2020


คำตอบ (2)


แต่ละรายการเรื่องราวดูเหมือนจะเป็นรายการที่มีสตริง

ใช้แผนที่เพื่อรับ สตริงจากรายการ ก่อนใช้ str ดังนี้

CCwordsCount = df4.story.map(lambda x: ''.join(x[1:-1])).str.count('|'.join(selected_words))

print(CCwordsCount.head(20))   # Show first 20 story results

เอาต์พุต

0      1
1      2
2      5
3      7
4      0
5      1
6     10
7      8
8      2
9      2
10     8
11     0
12     0
13     2
14     0
15     4
16     2
17     9
18     0
19     0
Name: story, dtype: int64

คำอธิบาย

แต่ละเรื่องราวอยู่ในรายการที่ถูกแปลงเป็นสตริง ดังนั้นโดยพื้นฐานแล้ว:

"['it', 'was', 'a', 'curious', 'choice', 'for', 'the', 'good', 'wife', ...]"

แปลงเป็นรายการคำโดยการวาง '[' และ ']' และเชื่อมคำเข้าด้วยกัน

map(lambda x: ''.join(x[1:-1]))

ซึ่งส่งผลให้คำคั่นด้วยเครื่องหมายจุลภาคในเครื่องหมายคำพูด สำหรับแถวแรกผลลัพธ์ที่ได้จะเป็นสตริง:

'it', 'was', 'a', 'curious', 'choice', 'for', ...
person DarrylG    schedule 13.05.2020
comment
@ Jesse-BurtonNicholson ตรวจสอบเรื่องราวของคุณเรื่องใดเรื่องหนึ่ง มีคำใดที่เลือกไว้หรือไม่? - person DarrylG; 13.05.2020
comment
ใช่ มีเรื่องราวมากกว่า 400 เรื่องและส่วนใหญ่มี Select_words อย่างน้อยหนึ่งรายการ - person Jesse-Burton Nicholson; 13.05.2020
comment
@ Jesse-BurtonNicholson - เวอร์ชันย่อของเรื่องราวที่คุณระบุไม่มีคำที่เลือก คุณช่วยบอกเรื่องราวเพิ่มเติมให้ฉันทดสอบได้ไหม นอกจากนี้ เมื่อฉันเพิ่มคำบางคำจากเรื่องราวในโพสต์ของคุณไปยังคำที่เลือก ฉันจะนับจำนวนที่ไม่เป็นศูนย์ - person DarrylG; 13.05.2020
comment
ฉันได้อัปเดตโพสต์พร้อมลิงก์ไปยังไฟล์ .csv ที่ส่งออกสำหรับ df4 แล้ว - person Jesse-Burton Nicholson; 13.05.2020
comment
BTW ขอบคุณมากสำหรับความช่วยเหลือของคุณในเรื่องนี้ รหัสของฉันใช้งานได้เมื่อปลายปีที่แล้ว และฉันเพิ่งกลับมาเพื่ออัปเดตโปรเจ็กต์ แต่มันไม่ได้ผล มันน่าหงุดหงิดมาก - person Jesse-Burton Nicholson; 13.05.2020
comment
@ Jesse-BurtonNicholson ดีใจที่ได้ช่วย อย่าลืมจะทำอย่างไรเมื่อมีคนตอบคำถามของฉัน - person DarrylG; 13.05.2020

ฟังก์ชัน .find() มีประโยชน์ และสิ่งนี้สามารถนำไปใช้ได้หลายวิธี หากคุณไม่มีจุดประสงค์อื่นสำหรับบทความดิบและอาจเป็นสตริงจำนวนมากได้ จากนั้นลองทำเช่นนี้ คุณสามารถใส่ไว้ในพจนานุกรมและวนซ้ำได้

def find_words(text, words):
    return [word for word in words if word in text]

sentences = "0  [it, was, a, curious, choice, for, the, good, 1      [when, he, was, a, yale, law, school, student, 2      [video, bitcoin, has, real, world, investors, 3      [bitcoin, s, wild, ride, may, not, have, been, 4      [amid, the, incense, cheap, art, and, herbal, 5      [san, francisco, eight, years, ago, ernie, all"

search_keywords=['accept', 'believe', 'trust', 'accepted', 'accepts', 'trusts', 'believes', \
                  'acceptance', 'trusted', 'trusting', 'accepting', 'believes', 'believing', 'believed',\
                 'normal', 'normalize', ' normalized', 'routine', 'belief', 'faith', 'confidence', 'adoption', \
                  'adopt', 'adopted', 'embrace', 'approve', 'approval', 'approved', 'approves', 'good']

found = find_words(sentences, search_keywords)

print(found)

หมายเหตุ: ฉันไม่มีกรอบข้อมูลแพนด้าในใจว่าฉันสร้างสิ่งนี้ขึ้นมา

person parlad    schedule 13.05.2020