简体   繁体   English

如何操作数据框,以便我访问单元格内列表中的每个元素并根据另一列对它们进行分组?

[英]How can manipulate a dataframe such that i access every element in a list inside a cell and group them according to another column?

this might be confusing so this is a copy of the first 5 rows of the data frame.这可能会令人困惑,所以这是数据框前 ​​5 行的副本。

number  cap words
0   ['Ages', 'Online', 'Python', 'Coding', 'CoursesAdwwwcodetodaycoukLearn', 'Python', 'Live', 'Taught', 'Experts', 'Making', 'Coding', 'Fun', 'Courses', 'Summer', 'Weekly', 'EthosSimple', 'Low', 'Cost', 'PricingFAQAccess', 'Free', 'Content']
1   ['Become', 'Python', 'Programmer', 'Study', 'Python', 'Online', 'FreeAdwwwpythoninstituteorgLearn', 'Python', 'Become', 'Python', 'Certified', 'Take', 'Your', 'Career', 'Next', 'Level', 'Kostenfreie', 'Lernplattform', 'Tausende', 'Studenten', 'Lass', 'Dich', 'Highlights', 'Offering', 'SelfStudy', 'Courses', 'Free', 'Courses', 'Available', 'Flexible', 'DeadlinesResources', 'Free', 'Education', 'Platform', 'Get', 'Certification', 'About']
2   ['Python', 'For', 'Beginners', 'Pythonorgwwwpythonorg', 'Python', 'Its', 'NonProgrammers', 'Python', 'Programmers', 'Python', 'Frequently', 'Asked', 'Books']
3   ['People', 'Python', 'I', 'PythonIs', 'Python', 'Python']
4   ['PythonHighlevel', 'Created', 'Guido', 'Rossum', 'Pythons', 'WikipediaTyping', 'Duck', 'July', 'August', 'Guido', 'RossumOS', 'Linux', 'Windows', 'Vista', 'IDEsIDLEPyCharmMicrosoft', 'Visual', 'StudioSpyderEclipsePyDevPeople']
5   ['Welcome', 'PythonorgwwwpythonorgThe', 'Python', 'Programming', 'Language', 'Python', 'For', 'Beginners', 'Beginners', 'Guide', 'Python', 'Docs', 'Python', 'Books']
6   ['BeginnersGuide', 'Python', 'Wikiwikipythonorg', 'BeginnersGuide4', 'Jul', 'New', 'Python', 'This', 'Chinese']
7   ['Learn', 'Python', 'Codecademywwwcodecademycom', 'Python', 'By']
8   ['Python', 'Wikipediaenwikipediaorg', 'PythonprogramminglanguagePython', 'Created', 'Guido', 'Rossum', 'Pythons', 'History', 'Features', 'Syntax', 'Python', 'Developer', 'Python', 'Software', 'Foundation', 'Paradigm', 'Multiparadigm', 'Designed', 'Guido', 'Rossum', 'Typing', 'Duck']
9   ['Related']

I am trying to unpack every word from the list inside the cell and group them all under their shared index number.我试图从单元格内的列表中解压缩每个单词,并将它们全部分组在它们的共享索引号下。

so like that就这样

    0
0   Ages
0   Online
0   Python
0   Coding
0   CoursesAdwwwcodetodaycoukLearn
0   Python
0   Live
0   Taught
0   Experts
0   Making
0   Coding
0   Fun
0   Courses
0   Summer
0   Weekly
0   EthosSimple
0   Low
0   Cost
0   PricingFAQAccess
0   Free
0   Content

and below follows 1 for the words 'Become', 'Python', 'Programmer', 'Study', 'Python', 'Online' etc...下面的 1 表示“Become”、“Python”、“Programmer”、“Study”、“Python”、“Online”等词...

I hope this is clear.我希望这很清楚。

Thanks谢谢

You can use explode你可以使用explode

x = [['Ages', 'Online', 'Python', 'Coding', 'CoursesAdwwwcodetodaycoukLearn', 'Python', 'Live', 'Taught', 'Experts', 'Making', 'Coding', 'Fun', 'Courses', 'Summer', 'Weekly', 'EthosSimple', 'Low', 'Cost', 'PricingFAQAccess', 'Free', 'Content']
,['Become', 'Python', 'Programmer', 'Study', 'Python', 'Online', 'FreeAdwwwpythoninstituteorgLearn', 'Python', 'Become', 'Python', 'Certified', 'Take', 'Your', 'Career', 'Next', 'Level', 'Kostenfreie', 'Lernplattform', 'Tausende', 'Studenten', 'Lass', 'Dich', 'Highlights', 'Offering', 'SelfStudy', 'Courses', 'Free', 'Courses', 'Available', 'Flexible', 'DeadlinesResources', 'Free', 'Education', 'Platform', 'Get', 'Certification', 'About']
,['Python', 'For', 'Beginners', 'Pythonorgwwwpythonorg', 'Python', 'Its', 'NonProgrammers', 'Python', 'Programmers', 'Python', 'Frequently', 'Asked', 'Books']
,['People', 'Python', 'I', 'PythonIs', 'Python', 'Python']
,['PythonHighlevel', 'Created', 'Guido', 'Rossum', 'Pythons', 'WikipediaTyping', 'Duck', 'July', 'August', 'Guido', 'RossumOS', 'Linux', 'Windows', 'Vista', 'IDEsIDLEPyCharmMicrosoft', 'Visual', 'StudioSpyderEclipsePyDevPeople']
,['Welcome', 'PythonorgwwwpythonorgThe', 'Python', 'Programming', 'Language', 'Python', 'For', 'Beginners', 'Beginners', 'Guide', 'Python', 'Docs', 'Python', 'Books']
,['BeginnersGuide', 'Python', 'Wikiwikipythonorg', 'BeginnersGuide4', 'Jul', 'New', 'Python', 'This', 'Chinese']
,['Learn', 'Python', 'Codecademywwwcodecademycom', 'Python', 'By']
,['Python', 'Wikipediaenwikipediaorg', 'PythonprogramminglanguagePython', 'Created', 'Guido', 'Rossum', 'Pythons', 'History', 'Features', 'Syntax', 'Python', 'Developer', 'Python', 'Software', 'Foundation', 'Paradigm', 'Multiparadigm', 'Designed', 'Guido', 'Rossum', 'Typing', 'Duck']
,['Related']]

df = pd.DataFrame({
    'number': np.arange(10),
    'cap words' :pd.Series(x)
})

df.explode('cap words').reset_index(drop=True)

Out:出去:

     number                       cap words
0         0                            Ages
1         0                          Online
2         0                          Python
3         0                          Coding
4         0  CoursesAdwwwcodetodaycoukLearn
..      ...                             ...
140       8                           Guido
141       8                          Rossum
142       8                          Typing
143       8                            Duck
144       9                         Related

[145 rows x 2 columns]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问另一个列表中每个列表的第n个元素? - How to access the nth element of every list inside another list? 如何将 Pandas 中的数据框列作为单元格列表插入到另一个数据框中? - How can I insert a column of a dataframe in pandas as a list of a cell into another dataframe? 如何对照另一个列检查pandas数据框列的值并在第三列中操作该值 - How can I check the value of a pandas dataframe column against another column and manipulate the value in a third column 如何根据另一列中的值检索 dataframe 列中的元素并将其存储在列表中 - How to retrieve element in dataframe column according to its value in another column ans store it in a list 如何将列表操作为一列? - How can I manipulate a list into a column? 如何根据dataframe python中列中的列表值进行分组 - How to group by according to the values of a list present in a column in dataframe python 如何根据 pandas dataframe 中的另一列在一列中填充 null 值? - How can I fill null values in one column according to another column in pandas dataframe? 如何将一个列表中的每个元素乘以另一个列表中的每个元素? - How can I multiply every element in one list by every element in another list? 如何为Pandas列中的每个元素分配一个列表(或系列)? - How can I assign a list (or series) to every element in column in Pandas? 如何根据条件用另一个列表中的元素替换列表中的某个元素 - How can i replace a certain element in a list with an element from another list according to a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM