简体   繁体   English

如何根据列表值创建修改后的 dataframe?

[英]How to create modified dataframe based on list values?

Consider a dataframe df of the following structure:-考虑以下结构的 dataframe df :-

Name    Slide          Height      Weight       Status       General
A           X             3            0.1             0.5       0.2
B           Y            10            0.2             0.7       0.8
    ...

I would like to create duplicates for each row in this dataframe (specific to the Name and Slide ) for the following combinations of Height and Weight shown by this list:-我想为此列表中显示的以下HeightWeight组合为此 dataframe (特定于NameSlide )中的每一行创建重复项:-

 list_combinations =  [[3,0.1],[10,0.2],[5,1.3]]

The desired output:-所需的 output:-

Name    Slide          Height      Weight       Status       General
A           X             3            0.1             0.5       0.2 #original 
A           X             10           0.2             0.5       0.2 # modified duplicate
A           X             5            1.3             0.5       0.2 # modified duplicate
B           Y            10            0.2             0.7       0.8 #original 
B           Y             3            0.1             0.7       0.8 # modified duplicate
B           Y             5            1.3             0.7       0.8 # modified duplicate
  etc.  ...

Any suggestions and help would be much appreciated.任何建议和帮助将不胜感激。

We can do merge with cross我们可以用cross merge

out = pd.DataFrame(list_combinations,columns = ['Height','Weight']).\
          merge(df,how='cross',suffixes = ('','_')).\         
               reindex(columns=df.columns).sort_values('Name')
  Name Slide  Height  Weight  Status  General
0    A     X       3     0.1     0.5      0.2
2    A     X      10     0.2     0.5      0.2
4    A     X       5     1.3     0.5      0.2
1    B     Y       3     0.1     0.7      0.8
3    B     Y      10     0.2     0.7      0.8
5    B     Y       5     1.3     0.7      0.8

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

相关问题 如何根据值创建修改后的列 - How to create modified column based on values 如何根据 dataframe 列中存储的列表值创建多行? - How to create multiple rows based on list values stores in column in dataframe? 根据条件和值列表创建 pyspark 数据框 - create pyspark dataframe based on condition and list of values 如何根据 Pandas 中的条件创建 dataframe 行的修改副本? - How to create modified copy of dataframe rows based on conditions in Pandas? 如何根据python中一行的一列DataFrame中的值创建二进制值列表? - How to create list of binary values based on values in one column DataFrame on one row in python? 如何基于将现有列值与值列表匹配来简洁地创建新的 dataframe 列? - how do I succinctly create a new dataframe column based on matching existing column values with list of values? 如何根据列表阈值过滤数据帧 - How to filter a dataframe based on a list threshold values 如何根据数据框中的 2 个条件创建列表? - How to create a list based on 2 conditions from a dataframe? 如何创建一个 lambda function 以根据条件和列表中的存在对 dataframe 值求和 - How to create a lambda function to sum dataframe values based on criteria and presence in a list Pandas:根据我的 dataframe 中的其他值列表创建一个新列 - Pandas: Create a new column based on a list of other values in my dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM