使用布尔值选择数据
我有这些数据:
Time Filename 60Ni 61Ni 62Ni 63Cu 64Ni 65Cu 66Zn
0 input/25.03.2022/220310001_Blk.TXT 0.004213561117649 0.0004941140553758 0.0008264054505464 0.0031620886176824 0.0027940864441916 0.0015828267166701 0.0016676299002331
0 input/25.03.2022/220310002_s01.TXT 10.450854313373563 0.4648053120821714 1.515178832411766 6.799761742353439 0.4018541350960731 3.1670226126909258 -0.0004149001091036
0 input/25.03.2022/220310003_Blk.TXT 0.0046728020068258 0.0005113642575452 0.0008742292275807 0.0038990775356069 0.0016948795023684 0.0018952833449778 0.0010220745559005
0 input/25.03.2022/220310004_NBS1.TXT 10.198064727783205 0.4535825699567795 1.4786694407463077 6.644256343841553 0.3992856484651566 3.094865655899048 0.0037580380868166
0 input/25.03.2022/220310004_NBS1.TXT 10.079309902191165 0.4483201849460602 1.46148056268692 6.564267292022705 0.3941892045736312 3.0576720571517946 0.0034487369190901
0 input/25.03.2022/220310004_NBS1.TXT 10.055493621826171 0.4472497546672821 1.4580038189888 6.549974250793457 0.3931813925504684 3.050980033874512 0.0033985046250745
0 input/25.03.2022/220310005_Blk.TXT 0.0054564320637534 0.000552704881799 0.0009742037393152 0.0051360657749076 0.0016950297751463 0.0024249535597239 0.0009928302722983
0 input/25.03.2022/220310006_s02.TXT 10.01531049013138 0.4454658553004265 1.4522675812244414 6.514146220684052 0.386061905324459 3.0344271540641783 0.000107468456008
0 input/25.03.2022/220310007_Blk.TXT 0.0058440430865933 0.0005767503850317 0.0010166236781515 0.005754723213613 0.0016812159873855 0.0026916580740362 0.0009854609476557
0 input/25.03.2022/220310008_IC11.TXT 9.994083042144776 0.4445306330919266 1.4492901706695556 7.470176630020141 0.3894518518447876 3.4796351766586304 0.0025492239557206
0 input/25.03.2022/220310008_IC11.TXT 9.96211524963379 0.4431046521663666 1.4446095180511476 7.446916389465332 0.3881216913461685 3.4687003660202027 0.0025008138502016
0 input/25.03.2022/220310008_IC11.TXT 9.986034145355225 0.4441730308532715 1.4480724453926086 7.463679447174072 0.3892696779966354 3.4765151739120483 0.0026400250289589
0 input/25.03.2022/220310009_Blk.TXT 0.0063325553511579 0.0006097427569329 0.0010847105528227 0.0069349051453173 0.001787266593116 0.0031935206692044 0.0010376764985267
0 input/25.03.2022/220310010_IC21.TXT 9.907929096221924 0.4407092106342316 1.436798827648163 6.06647294998169 0.381802784204483 2.8266239309310914 -1.9165552830600065e-05
0 input/25.03.2022/220310010_IC21.TXT 9.898069801330566 0.440289745926857 1.4353946447372437 6.061149816513062 0.381249166727066 2.824158968925476 -0.000127439211501
0 input/25.03.2022/220310010_IC21.TXT 9.873182182312013 0.4391724157333374 1.4317991995811463 6.046364660263062 0.3803336149454117 2.8172996759414675 -0.0001105861682299
0 input/25.03.2022/220310011_Blk.TXT 0.006776000233367 0.0006151825053772 0.0011437231053908 0.0076008092767248 0.0019233425652297 0.0034851033162946 0.0010968211592019
0 input/25.03.2022/220310012_s03.TXT 9.919624531269074 0.4412683457136154 1.4386062547564509 6.453964179754257 0.3823180440813303 3.006967249512672 -5.925155056729638e-06
0 input/25.03.2022/220310013_Blk.TXT 0.0070609439785281 0.000630383901686 0.0011728596175089 0.0080440094384054 0.002036696835421 0.0036809448773662 0.0011725965615672
0 input/25.03.2022/220310014_NBS2.TXT 9.981303691864014 0.4439967930316925 1.4475916290283204 6.529758939743042 0.3842234253883362 3.0423321390151976 -0.0003124714840669
0 input/25.03.2022/220310014_NBS2.TXT 9.914944686889648 0.4410672289133072 1.43802725315094 6.483507022857666 0.3816052615642548 3.020912132263184 -0.0003612653422169
0 input/25.03.2022/220310014_NBS2.TXT 9.886322116851806 0.4397846907377243 1.433876838684082 6.466586313247681 0.3804377472400665 3.012995510101318 -0.0004034811019664
0 input/25.03.2022/220310015_Blk.TXT 0.0073987463644395 0.0006374313224417 0.00121240914256 0.0086917337340613 0.0023169121006503 0.0039539939956739 0.0013138331084822
0 input/25.03.2022/220310016_IC12.TXT 9.85557996749878 0.4384415912628174 1.429577150344849 7.381738519668579 0.3795971584320068 3.4393920421600344 -0.0002216772679821
0 input/25.03.2022/220310016_IC12.TXT 9.82486707687378 0.4370819771289825 1.4251339054107666 7.356578054428101 0.3785045564174652 3.4277337741851808 -0.0001708243161556
0 input/25.03.2022/220310016_IC12.TXT 9.80790719985962 0.4363300800323486 1.4227154779434203 7.344034671783447 0.3781744015216827 3.421962966918945 4.260804889781866e-06
0 input/25.03.2022/220310017_Blk.TXT 0.0075777747668325 0.0006548739698094 0.0012614171913204 0.0094458545868595 0.0020529380068182 0.0042742350138723 0.0011683187098242
0 input/25.03.2022/220310018_s04.TXT 9.812697958946227 0.4365527924150228 1.4234635725617408 6.387677395343781 0.3783831790089607 2.9768310248851777 -2.183340569104075e-05
0 input/25.03.2022/220310019_Blk.TXT 0.0078119638841599 0.0006677620811387 0.001280953696308 0.0097443414541582 0.0025180794376259 0.0044003704718003 0.0014272088340173
我读了它,然后删除了我不想要的所有内容::
import pandas as pd
df = pd.read_csv(fullname, delimiter='\t', index_col=False)
df.drop(['Time', '60Ni', '61Ni', '62Ni', '64Ni', '66Zn'], axis=1, inplace=True)
插入比率:
df['Cu_ratio'] = df['65Cu'] / df['63Cu']
按名称检测行:
df['blank']=df['Filename'].str.contains('_Blk|_BLK')
df['standard']=df['Filename'].str.contains('_s')
df['sample']=~df['blank'] & ~df['standard']
dfSamples=df[df['sample'] == True]
每个“样本”之前和之后检测“标准”
i = 0
for sampleIndex in dfSamples.index:
dfBefore=df.iloc[0:sampleIndex]
dfBeforeInverse=dfBefore[::-1]
dfStandardBefore = dfBeforeInverse[dfBeforeInverse['standard'] == True]
indexBefore = dfStandardBefore.index[0]
df.at[sampleIndex, 'standardBefore'] = indexBefore
lastIndex = df.index[-1]
dfAfter=df.iloc[sampleIndex:lastIndex]
dfStandardAfter = dfAfter[dfAfter['standard'] == True]
indexAfter = dfStandardAfter.index[0]
df.at[sampleIndex, 'standardAfter'] = indexAfter
然后我需要在到目前为止有效的
。但我正在努力的地方:我需要进行计算: 首先,我需要计算每个“样本”之前和之后的“标准”的平均值:
我尝试过
av_std = sum(dfStandardBefore['Cu_ratio'], dfStandardAfter['Cu_ratio']) / 2
,但它计算之前和之后的所有标准的总和,而不仅仅是用布尔值掩盖的标准。我需要的例如:
NBS1 的 s01 和 s02; s02和s03用于IC11和IC21,s03和s04用于NBS2和IC12,依此类推。
最后一步是对每个样本进行计算:
delta = (av_std / sample) * 1000
并将其导出到 csv 中。
我该如何解决这个问题?
I have this data:
Time Filename 60Ni 61Ni 62Ni 63Cu 64Ni 65Cu 66Zn
0 input/25.03.2022/220310001_Blk.TXT 0.004213561117649 0.0004941140553758 0.0008264054505464 0.0031620886176824 0.0027940864441916 0.0015828267166701 0.0016676299002331
0 input/25.03.2022/220310002_s01.TXT 10.450854313373563 0.4648053120821714 1.515178832411766 6.799761742353439 0.4018541350960731 3.1670226126909258 -0.0004149001091036
0 input/25.03.2022/220310003_Blk.TXT 0.0046728020068258 0.0005113642575452 0.0008742292275807 0.0038990775356069 0.0016948795023684 0.0018952833449778 0.0010220745559005
0 input/25.03.2022/220310004_NBS1.TXT 10.198064727783205 0.4535825699567795 1.4786694407463077 6.644256343841553 0.3992856484651566 3.094865655899048 0.0037580380868166
0 input/25.03.2022/220310004_NBS1.TXT 10.079309902191165 0.4483201849460602 1.46148056268692 6.564267292022705 0.3941892045736312 3.0576720571517946 0.0034487369190901
0 input/25.03.2022/220310004_NBS1.TXT 10.055493621826171 0.4472497546672821 1.4580038189888 6.549974250793457 0.3931813925504684 3.050980033874512 0.0033985046250745
0 input/25.03.2022/220310005_Blk.TXT 0.0054564320637534 0.000552704881799 0.0009742037393152 0.0051360657749076 0.0016950297751463 0.0024249535597239 0.0009928302722983
0 input/25.03.2022/220310006_s02.TXT 10.01531049013138 0.4454658553004265 1.4522675812244414 6.514146220684052 0.386061905324459 3.0344271540641783 0.000107468456008
0 input/25.03.2022/220310007_Blk.TXT 0.0058440430865933 0.0005767503850317 0.0010166236781515 0.005754723213613 0.0016812159873855 0.0026916580740362 0.0009854609476557
0 input/25.03.2022/220310008_IC11.TXT 9.994083042144776 0.4445306330919266 1.4492901706695556 7.470176630020141 0.3894518518447876 3.4796351766586304 0.0025492239557206
0 input/25.03.2022/220310008_IC11.TXT 9.96211524963379 0.4431046521663666 1.4446095180511476 7.446916389465332 0.3881216913461685 3.4687003660202027 0.0025008138502016
0 input/25.03.2022/220310008_IC11.TXT 9.986034145355225 0.4441730308532715 1.4480724453926086 7.463679447174072 0.3892696779966354 3.4765151739120483 0.0026400250289589
0 input/25.03.2022/220310009_Blk.TXT 0.0063325553511579 0.0006097427569329 0.0010847105528227 0.0069349051453173 0.001787266593116 0.0031935206692044 0.0010376764985267
0 input/25.03.2022/220310010_IC21.TXT 9.907929096221924 0.4407092106342316 1.436798827648163 6.06647294998169 0.381802784204483 2.8266239309310914 -1.9165552830600065e-05
0 input/25.03.2022/220310010_IC21.TXT 9.898069801330566 0.440289745926857 1.4353946447372437 6.061149816513062 0.381249166727066 2.824158968925476 -0.000127439211501
0 input/25.03.2022/220310010_IC21.TXT 9.873182182312013 0.4391724157333374 1.4317991995811463 6.046364660263062 0.3803336149454117 2.8172996759414675 -0.0001105861682299
0 input/25.03.2022/220310011_Blk.TXT 0.006776000233367 0.0006151825053772 0.0011437231053908 0.0076008092767248 0.0019233425652297 0.0034851033162946 0.0010968211592019
0 input/25.03.2022/220310012_s03.TXT 9.919624531269074 0.4412683457136154 1.4386062547564509 6.453964179754257 0.3823180440813303 3.006967249512672 -5.925155056729638e-06
0 input/25.03.2022/220310013_Blk.TXT 0.0070609439785281 0.000630383901686 0.0011728596175089 0.0080440094384054 0.002036696835421 0.0036809448773662 0.0011725965615672
0 input/25.03.2022/220310014_NBS2.TXT 9.981303691864014 0.4439967930316925 1.4475916290283204 6.529758939743042 0.3842234253883362 3.0423321390151976 -0.0003124714840669
0 input/25.03.2022/220310014_NBS2.TXT 9.914944686889648 0.4410672289133072 1.43802725315094 6.483507022857666 0.3816052615642548 3.020912132263184 -0.0003612653422169
0 input/25.03.2022/220310014_NBS2.TXT 9.886322116851806 0.4397846907377243 1.433876838684082 6.466586313247681 0.3804377472400665 3.012995510101318 -0.0004034811019664
0 input/25.03.2022/220310015_Blk.TXT 0.0073987463644395 0.0006374313224417 0.00121240914256 0.0086917337340613 0.0023169121006503 0.0039539939956739 0.0013138331084822
0 input/25.03.2022/220310016_IC12.TXT 9.85557996749878 0.4384415912628174 1.429577150344849 7.381738519668579 0.3795971584320068 3.4393920421600344 -0.0002216772679821
0 input/25.03.2022/220310016_IC12.TXT 9.82486707687378 0.4370819771289825 1.4251339054107666 7.356578054428101 0.3785045564174652 3.4277337741851808 -0.0001708243161556
0 input/25.03.2022/220310016_IC12.TXT 9.80790719985962 0.4363300800323486 1.4227154779434203 7.344034671783447 0.3781744015216827 3.421962966918945 4.260804889781866e-06
0 input/25.03.2022/220310017_Blk.TXT 0.0075777747668325 0.0006548739698094 0.0012614171913204 0.0094458545868595 0.0020529380068182 0.0042742350138723 0.0011683187098242
0 input/25.03.2022/220310018_s04.TXT 9.812697958946227 0.4365527924150228 1.4234635725617408 6.387677395343781 0.3783831790089607 2.9768310248851777 -2.183340569104075e-05
0 input/25.03.2022/220310019_Blk.TXT 0.0078119638841599 0.0006677620811387 0.001280953696308 0.0097443414541582 0.0025180794376259 0.0044003704718003 0.0014272088340173
I read it and I drop everything I don't want::
import pandas as pd
df = pd.read_csv(fullname, delimiter='\t', index_col=False)
df.drop(['Time', '60Ni', '61Ni', '62Ni', '64Ni', '66Zn'], axis=1, inplace=True)
inserting a ratio:
df['Cu_ratio'] = df['65Cu'] / df['63Cu']
detecting rows by name:
df['blank']=df['Filename'].str.contains('_Blk|_BLK')
df['standard']=df['Filename'].str.contains('_s')
df['sample']=~df['blank'] & ~df['standard']
dfSamples=df[df['sample'] == True]
then I need to detect the "standard" before and after each "sample"
i = 0
for sampleIndex in dfSamples.index:
dfBefore=df.iloc[0:sampleIndex]
dfBeforeInverse=dfBefore[::-1]
dfStandardBefore = dfBeforeInverse[dfBeforeInverse['standard'] == True]
indexBefore = dfStandardBefore.index[0]
df.at[sampleIndex, 'standardBefore'] = indexBefore
lastIndex = df.index[-1]
dfAfter=df.iloc[sampleIndex:lastIndex]
dfStandardAfter = dfAfter[dfAfter['standard'] == True]
indexAfter = dfStandardAfter.index[0]
df.at[sampleIndex, 'standardAfter'] = indexAfter
this works so far.
But where I'm struggling: I need to make a calculation:
First, I need to calculate the average of the "standard" before and after each "sample":
I tried it with
av_std = sum(dfStandardBefore['Cu_ratio'], dfStandardAfter['Cu_ratio']) / 2
but it calculates the sum of ALL standards before and after, not only the masked with boolean. What I need it to be for example:
s01 and s02 for NBS1; s02 and s03 for IC11 and IC21, s03 and s04 for NBS2 and IC12, and so on.
The last step would be to make a calculation for each sample:
delta = (av_std / sample) * 1000
and export it into a csv.
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论