基于另一个数据帧中的值替换数据帧中行中的值(Python)

2024-05-16 22:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有2个数据帧(阈值和信息表)第一行是一行标题:

(Thresholds)

AA BB CC DD EE 
0  15 7  0  23

以及

(InfoTable)

ID Xposition Yposition AA BB CC DD EE
1  1         1         10 20 5  10 50
2  2         2         20 12 10 20 2
3  3         3         30 19 17 30 26
4  4         4         40 35 3  40 38
5  5         5         50 16 5  50 16

我正在尝试筛选数据,以便Thresholds数据框中包含0的列是从InfoTable数据框中删除的列。然后,我尝试将Thresholds数据框中每一行的值与InfoTable数据框中的值进行比较,以便可以用InfoTable中的1或0来替换它们。我的期望输出如下:

ID Xposition Yposition BB CC EE
1  1         1         1  0  1
2  2         2         0  1  0
3  3         3         1  1  1
4  4         4         1  0  1
5  5         5         1  0  0

这是我现在用来过滤每个表的代码。你知道吗

with open('thresholds_test.txt' ) as a:
    Thresholds = pd.read_table(a, sep=',') 
print Thresholds 

with open('includedThresholds.txt') as b:
    IncludedThresholds = pd.read_table(b, sep=',' )
print IncludedThresholds

InterestingThresholds = IncludedThresholds.drop(IncludedThresholds.columns[~IncludedThresholds.iloc[0].astype(bool)],axis=1)
print InterestingThresholds 

with open('PivotTable.tab') as c:
    PivotTable = pd.read_table(c, sep='\t' )
print PivotTable

headers = InterestingThresholds.columns.append(pd.Index(['ID','XPostion','YPosition']))
InfoTable = PivotTable.loc[:, headers]
print InfoTable 

任何帮助都将不胜感激!你知道吗


Tags: 数据idreadaswithopeneecc
1条回答
网友
1楼 · 发布于 2024-05-16 22:18:58

查找要保留和删除的列:

cols = Thresholds.columns[Thresholds.iloc[0].astype(bool)]
dcols = Thresholds.columns[~Thresholds.iloc[0].astype(bool)]

进行比较:

comp_df = pd.DataFrame(InfoTable[cols].values >= Thresholds[cols].values, columns=cols).astype(int)

将比较结果分配给原始dataframe和drop列:

df_out = InfoTable.assign(**comp_df).drop(dcols, axis=1)
print(df_out)

输出:

  ID  Xposition  Yposition  BB  CC  EE
0   1          1          1   1   0   1
1   2          2          2   0   1   0
2   3          3          3   1   1   1
3   4          4          4   1   0   1
4   5          5          5   1   0   0

相关问题 更多 >