简体   繁体   English

通过对现有数据框应用逻辑来创建新的 Pandas 数据框

[英]Creating a new pandas dataframe by applying logic to already existing dataframes

I have two pandas dataframes as follows.我有两个熊猫数据框如下。

data_1= {'features_names': ['F1','F2','F3','F4'],
        'Sample_1': [2260,25000,27000,35000],
        'Sample_2': [22000,25,8,35000],
         'Sample_3': [2350,25000,27000,3900],
         'Sample_4': [25000,2570,250,3000]

        }
df_1 = pd.DataFrame(data_1)

在此处输入图片说明

and another data frame as follows.和另一个数据框如下。

data_2={'Sample_name': ['Sample_2','Sample_3','Sample_4','Sample_1'],
        'class': ['class_1','class_1','class_2','class_3'],
        'sex': ['m','m','f','m'],
         'age': [23,25,21,35],
         'RIN': [2.5,2.8,3.8,3.0]

        }
df_2 = pd.DataFrame(data_2)

在此处输入图片说明

Now using df_1 and df_2 , I want to create df_3 which should be as follows.现在使用df_1df_2 ,我想创建df_3 ,它应该如下所示。 在此处输入图片说明

I have done it manually with the following code.我已使用以下代码手动完成。

data_3= {
    'class': ['class_3','class_1','class_1','class_2'],

    'sex': ['m','m','f','f'],

    'age': [35,23,25,21],

    'RIN': [3.0,2.5,2.8,3.8],

    'features_names': ['F1','F2','F3','F4'],
        'Sample_1': [2260,25000,27000,35000],
        'Sample_2': [22000,25,8,35000],
         'Sample_3': [2350,25000,27000,3900],
         'Sample_4': [25000,2570,250,3000]

        }
df_3 = pd.DataFrame(data_3)

But in actual, I have a very large amount of data and doing it manually won't be possible.但实际上,我有大量的数据,手动操作是不可能的。 Is there any automatic way to do this.有没有自动的方法来做到这一点。

Use concat with sorted DataFrame by column Sample_name by DataFrame.sort_values and then remove column:按列Sample_name通过DataFrame.sort_values使用concat与排序的DataFrame.sort_values ,然后删除列:

df_3 = (pd.concat([df_2.sort_values('Sample_name').reset_index(drop=True), df_1], axis=1)
          .drop('Sample_name', axis=1))
print (df_3)

     class sex  age  RIN features_names  Sample_1  Sample_2  Sample_3  \
0  class_3   m   35  3.0             F1      2260     22000      2350   
1  class_1   m   23  2.5             F2     25000        25     25000   
2  class_1   m   25  2.8             F3     27000         8     27000   
3  class_2   f   21  3.8             F4     35000     35000      3900   

   Sample_4  
0     25000  
1      2570  
2       250  
3      3000  

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

相关问题 创建一个新的 pandas dataframe 从许多现有的 pandas 数据帧编译信息 - Creating a new pandas dataframe compiling information from many existing pandas dataframes 从现有的 dataframe pandas 创建新的 dataframe - creating new dataframe from existing dataframe pandas 熊猫数据框:通过查找和计算现有数据框来制作新数据框 - pandas dataframe: make new dataframe from lookup and computation of existing dataframes 将来自多个数据帧的新数据和现有数据添加到单个 dataframe 中? - Adding new and already existing data from multiple dataframes into a singular dataframe? 从现有的 dataframe 应用数据时间条件创建一个新的 dataframe - Creating a new dataframe from existing dataframe applying datatetime conditions 从现有数据库创建一个新的Pandas DataFrame - Creating a new Pandas DataFrame from an Existing One 从两个现有数据帧中创建一个新的 pandas dataframe - Create a new pandas dataframe out of two existing dataframes 使用数学计算从现有数据帧创建新数据帧 - Creating new dataframe from existing dataframes using a mathematical calculation 使用现有数据框中的相似值创建新数据框 - Creating new dataframes using like values in an existing dataframe 通过从其他数据框中提取列来创建新的熊猫数据框-ValueError - Creating new pandas dataframe by extracting columns from other dataframes - ValueError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM