[英]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_1
和df_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.