简体   繁体   English

从 Pandas 数据帧的每行多个值:获取包含每个值的每个实现的两列(使用 Networkx 分析网络)

[英]From multiple values per rows of a pandas dataframe: get two columns with every realation of the values (to analyse the network with Networkx)

I have a dataframe with names of persons in it.我有一个包含人名的数据框。 The persons work thogether on the same item.人们在同一个项目上一起工作。

item   names
a      moriz, jon, cate 
b      jon, lenard 
c      cate, martin, leo, jil 
  • I like to prepare the names for a network-visualisation.我喜欢为网络可视化准备名称。 I need to split the name-cells up in in two rows: in a way, that every relation is shown.我需要将名称单元格分成两行:以某种方式显示每个关系。 like this:像这样:
item    person 1    person 2
a       moriz       jon
a       moriz       cate
a       jon         cate
b       jon         lenard
c       cate        martin
c       cate        leo
c       cate        jil
c       jil         martin
c       jil         leo
c       martin      leo
  • I know how to split the name-cell in multiple name-cells for each item.我知道如何为每个项目将名称单元拆分为多个名称单元。 But I don't know how to list them in pairs with every relation per item.但我不知道如何将它们与每个项目的每个关系成对列出。

You could do something like this ( df your dataframe):你可以做这样的事情( df你的数据框):

from itertools import combinations

df.names = df.names.str.split(", ").map(lambda l: [*combinations(l, 2)])
df = df.explode("names")
df[["person 1", "person 2"]] = df.names.str.join(",").str.split(",", expand=True)
df = df.drop(columns="names")

Result for the sample:样品结果:

  item person 1 person 2
0    a    moriz      jon
0    a    moriz     cate
0    a      jon     cate
1    b      jon   lenard
2    c     cate   martin
2    c     cate      leo
2    c     cate      jil
2    c   martin      leo
2    c   martin      jil
2    c      leo      jil

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

相关问题 Pandas:分析多个 DataFrame 列中值的频率 - Pandas: Analyse frequency of values in multiple DataFrame columns 从 pandas dataframe 行中获取两个最大值的列名 - Get the columns name of the two largest values from pandas dataframe rows 从Pandas Dataframe,在某些列中具有共同值的不同行之间构建networkx图表或流程图 - From a Pandas Dataframe, build networkx chart or flow chart between different rows with common values in certain columns Pandas DataFrame:在两个特定的列中获取具有相同值对的行 - Pandas DataFrame: get rows with same pair of values in two specific columns 根据pandas中多列的值从Dataframe中选择行 - Selecting rows from a Dataframe based on values from multiple columns in pandas 根据具有相似值的多列从熊猫数据框中删除行 - Remove rows from pandas dataframe based on multiple columns with similar values 根据pandas中多列中的值从Dataframe中选择行 - Selecting rows from a Dataframe based on values in multiple columns in pandas 根据熊猫中MULTIPLE列中的值从DataFrame中选择行 - Select rows from a DataFrame based on values in a MULTIPLE columns in pandas 使用多列中的值进行过滤后,Python Pandas Dataframe获得行数 - Python Pandas Dataframe get count of rows after filtering using values from multiple columns 从行到 Pandas 数据帧的列的值 - Values from rows to columns of a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM