我有一个数据框字典。每一个都有大约30行。我正在创建一个新的数据框,新数据框中的每一行我都希望是索引以Tc
开始的所有行加上索引为Ru-097
的行的总和。我目前正在使用下面的代码来计算以Tc
开头的索引的行总数:
for i in df_dict:
xs_elem.loc[i] = df_dict[i][df_dict[i].index.str.startswith(inta)].sum(axis=0, skipna = True)
其中df_dict
是数据帧的原始字典,而xs_elem
是新数据帧。我目前正在努力锻炼如何现在从每个原始数据帧中添加索引为Ru-097
的行。我已经附上了原始字典中一个数据框的示例:
{'093Nb-a': 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 ... \
product ...
Mo-093 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Mo-093(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Mo-093(L16) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Mo-094 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Mo-095 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Mo-096 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-091(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-091(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-092 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-092(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-092(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-093(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-093(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-094(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Nb-094(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-093(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-093(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-094(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-094(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-095 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-095(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-095(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-096(L00) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
Tc-096(L01) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...
35.5 36.0 36.5 37.0 37.5 \
product
Mo-093 10.040000 14.632800 21.599300 32.06920 49.24130
Mo-093(L00) 9.744110 13.460200 17.185100 20.28300 27.32540
Mo-093(L16) 0.295912 1.172700 4.414180 11.78630 21.91600
Mo-094 196.562000 204.674000 210.715000 217.95200 224.37300
Mo-095 57.097900 54.979600 50.882000 49.70360 46.20760
Mo-096 3.978270 3.847120 3.752030 3.71361 3.63636
Nb-091(L00) 52.024500 53.725600 57.453200 57.88070 60.03960
Nb-091(L01) 17.239500 18.107100 20.356500 20.97550 21.67950
Nb-092 82.068500 82.352600 81.429500 78.72100 77.60420
Nb-092(L00) 58.983100 59.228600 58.089800 55.80080 54.81690
Nb-092(L01) 23.085400 23.124000 23.339800 22.92020 22.78720
Nb-093(L00) 40.232200 39.298800 39.472100 37.60480 37.53590
Nb-093(L01) 6.356590 6.371910 6.408030 5.82185 5.75791
Nb-094(L00) 0.933374 0.947151 1.165000 1.19548 1.42785
Nb-094(L01) 1.489570 1.385020 1.737430 1.63560 1.96983
Tc-093(L00) 3.000230 8.628490 17.486900 31.77190 46.47390
Tc-093(L01) 0.063022 0.201900 0.431364 1.14585 2.91240
Tc-094(L00) 895.490000 907.463000 916.315000 916.49400 902.47600
Tc-094(L01) 24.440700 23.595100 22.438600 20.92200 19.39410
Tc-095 172.828000 152.575000 129.593000 111.87600 96.75620
Tc-095(L00) 165.202000 145.326000 122.658000 105.17800 90.25420
Tc-095(L01) 7.625800 7.249500 6.934840 6.69773 6.50196
Tc-096(L00) 5.884210 5.643140 5.484500 5.34090 5.19044
Tc-096(L01) 1.794880 1.750060 1.739150 1.71570 1.70735
38.0 38.5 39.0 39.5 40.0
product
Mo-093 63.57020 89.75830 111.03700 145.84000 166.05700
Mo-093(L00) 29.22050 37.55490 39.94700 49.35280 52.41390
Mo-093(L16) 34.34970 52.20340 71.09020 96.48730 113.64400
Mo-094 228.25400 234.06500 239.76000 238.56400 236.34500
Mo-095 43.55800 42.00010 39.56730 38.08940 36.88660
Mo-096 3.49909 3.35616 3.29289 3.12873 3.01025
Nb-091(L00) 61.46730 64.53930 66.55750 69.13940 71.44250
Nb-091(L01) 22.83200 24.30250 25.53310 26.91970 28.45680
Nb-092 73.17120 72.87840 69.16360 68.70760 64.55220
Nb-092(L00) 51.50720 51.10550 48.53910 48.01630 44.93510
Nb-092(L01) 21.66400 21.77290 20.62450 20.69130 19.61710
Nb-093(L00) 36.25430 36.29230 35.42060 35.73530 35.10910
Nb-093(L01) 5.53577 5.55193 5.39226 5.42066 5.29100
Nb-094(L00) 1.45868 1.66030 1.72756 1.92365 2.05499
Nb-094(L01) 1.81729 2.10282 2.05614 2.29654 2.29348
Tc-093(L00) 75.29960 93.79430 131.43600 155.85000 198.16200
Tc-093(L01) 6.56885 9.81136 15.80680 21.01230 29.64780
Tc-094(L00) 877.68600 834.88300 783.80600 732.76100 676.82000
Tc-094(L01) 17.99270 16.90750 15.47530 14.18860 12.92230
Tc-095 86.07090 80.83800 73.93850 66.92640 63.73640
Tc-095(L00) 79.79190 74.78590 67.91930 61.08270 58.06560
Tc-095(L01) 6.27897 6.05212 6.01928 5.84365 5.67085
Tc-096(L00) 5.08840 4.81673 4.46807 4.29650 4.09283
Tc-096(L01) 1.68199 1.52316 1.35884 1.33841 1.29647
[24 rows x 80 columns],
'096Mo-p': 0.5 1.0 1.5 2.0 2.5 3.0 \
product
Mo-092 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-093 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-093(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-093(L16) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-094 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-095 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Mo-096 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.001947
Nb-090 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-090(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-090(L02) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-091(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-091(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-092 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-092(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-092(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-093(L00) 0.0 0.0 0.000000e+00 0.000000 3.795890e-09 0.000014
Nb-093(L01) 0.0 0.0 0.000000e+00 0.000000 4.981000e-07 0.000046
Nb-094(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-094(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-095 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Nb-095(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-093(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-093(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-094(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-094(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-095 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-095(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-095(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-096(L00) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-096(L01) 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Tc-097(L00) 0.0 0.0 1.475680e-09 0.002265 3.413350e-02 0.220070
Zr-090 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
Zr-091 0.0 0.0 0.000000e+00 0.000000 0.000000e+00 0.000000
3.5 4.0 4.5 5.0 ... 35.5 \
product ...
Mo-092 0.000000 0.000000 0.000000 0.000000 ... 0.194698
Mo-093 0.000000 0.000000 0.000000 0.000000 ... 81.630300
Mo-093(L00) 0.000000 0.000000 0.000000 0.000000 ... 74.056400
Mo-093(L16) 0.000000 0.000000 0.000000 0.000000 ... 7.573870
Mo-094 0.000000 0.000000 0.000000 0.000000 ... 261.417000
Mo-095 0.000000 0.000000 0.000000 0.000000 ... 221.953000
Mo-096 0.051812 0.062333 0.038230 0.125293 ... 107.862000
Nb-090 0.000000 0.000000 0.000000 0.000000 ... 0.122764
Nb-090(L00) 0.000000 0.000000 0.000000 0.000000 ... 0.093987
Nb-090(L02) 0.000000 0.000000 0.000000 0.000000 ... 0.028778
Nb-091(L00) 0.000000 0.000000 0.000000 0.000000 ... 27.828200
Nb-091(L01) 0.000000 0.000000 0.000000 0.000000 ... 10.945500
Nb-092 0.000000 0.000000 0.000000 0.000000 ... 11.128200
Nb-092(L00) 0.000000 0.000000 0.000000 0.000000 ... 7.450080
Nb-092(L01) 0.000000 0.000000 0.000000 0.000000 ... 3.678110
Nb-093(L00) 0.000258 0.000301 0.000112 0.000574 ... 3.921400
Nb-093(L01) 0.000761 0.000949 0.000442 0.001563 ... 0.858452
Nb-094(L00) 0.000000 0.000000 0.000000 0.000000 ... 2.803280
Nb-094(L01) 0.000000 0.000000 0.000000 0.000000 ... 3.109510
Nb-095 0.000000 0.000000 0.000000 0.000000 ... 5.090490
Nb-095(L00) 0.000000 0.000000 0.000000 0.000000 ... 4.780890
Tc-093(L00) 0.000000 0.000000 0.000000 0.000000 ... 170.967000
Tc-093(L01) 0.000000 0.000000 0.000000 0.000000 ... 17.761100
Tc-094(L00) 0.000000 0.000000 0.000000 0.000000 ... 272.981000
Tc-094(L01) 0.000000 0.000000 0.000000 0.000000 ... 24.393900
Tc-095 0.000000 0.000000 0.000000 0.000000 ... 116.815000
Tc-095(L00) 0.000000 0.000000 0.000000 0.000000 ... 101.107000
Tc-095(L01) 0.000000 0.000000 0.000000 0.000000 ... 15.708100
Tc-096(L00) 0.000000 3.516110 10.682500 23.264000 ... 22.490200
Tc-096(L01) 0.000000 4.191410 12.629300 27.733700 ... 8.400500
Tc-097(L00) 0.835016 0.192408 0.042151 0.054529 ... 0.252571
Zr-090 0.000000 0.000000 0.000000 0.000000 ... 1.376020
Zr-091 0.000000 0.000000 0.000000 0.000000 ... 0.756446
36.0 36.5 37.0 37.5 38.0 \
product
Mo-092 0.272634 0.364946 0.489507 0.713059 1.102390
Mo-093 98.401900 103.999000 119.331000 132.559000 142.175000
Mo-093(L00) 88.816300 93.446700 105.037000 115.843000 123.103000
Mo-093(L16) 9.585520 10.552800 14.293500 16.715700 19.072500
Mo-094 259.271000 259.456000 257.227000 254.758000 252.432000
Mo-095 221.619000 219.479000 220.937000 220.069000 221.639000
Mo-096 106.605000 106.636000 105.159000 104.590000 103.227000
Nb-090 0.200268 0.416199 0.837886 1.364180 1.957160
Nb-090(L00) 0.153469 0.345417 0.656147 1.043450 1.479270
Nb-090(L02) 0.046800 0.070782 0.181739 0.320725 0.477894
Nb-091(L00) 27.835600 27.695900 27.439700 27.346600 26.838200
Nb-091(L01) 10.998500 10.923500 10.819500 10.656100 10.467700
Nb-092 10.675500 10.260700 9.947860 9.268650 9.021180
Nb-092(L00) 7.127640 6.860240 6.659290 6.258230 6.148420
Nb-092(L01) 3.547840 3.400410 3.288570 3.010420 2.872750
Nb-093(L00) 4.216350 4.472430 4.807140 5.332180 5.787620
Nb-093(L01) 0.911584 0.981007 1.036370 1.092960 1.275550
Nb-094(L00) 3.025280 3.228980 3.434280 3.544840 3.657970
Nb-094(L01) 3.342570 3.523070 3.667980 3.720940 3.648950
Nb-095 5.396300 5.733150 6.187680 6.540620 6.927770
Nb-095(L00) 5.070740 5.390850 5.818430 6.150300 6.514400
Tc-093(L00) 179.691000 201.655000 207.974000 216.272000 226.337000
Tc-093(L01) 20.473700 22.198000 23.382300 25.810400 26.247200
Tc-094(L00) 253.503000 229.945000 211.226000 193.080000 175.816000
Tc-094(L01) 22.306800 20.196000 18.292900 16.571900 15.024400
Tc-095 112.903000 110.202000 108.205000 106.184000 104.493000
Tc-095(L00) 97.671800 95.428500 93.816100 92.163800 90.759900
Tc-095(L01) 15.230800 14.773600 14.388500 14.020400 13.733300
Tc-096(L00) 22.006100 21.458900 21.383600 21.027100 20.890300
Tc-096(L01) 8.251280 8.073800 8.105300 7.983600 8.019030
Tc-097(L00) 0.237078 0.234648 0.227061 0.223095 0.222145
Zr-090 1.842150 2.396170 2.952780 3.580560 4.283480
Zr-091 0.819205 0.882182 0.937785 0.988787 1.029840
38.5 39.0 39.5 40.0
product
Mo-092 1.760360 2.767710 4.212900 6.472470
Mo-093 156.465000 170.650000 179.715000 192.436000
Mo-093(L00) 134.558000 147.327000 154.414000 163.208000
Mo-093(L16) 21.907100 23.322400 25.300900 29.228800
Mo-094 251.026000 245.157000 243.405000 241.537000
Mo-095 220.090000 221.075000 218.101000 218.373000
Mo-096 102.861000 101.640000 101.468000 100.550000
Nb-090 2.652820 3.936520 5.671160 7.386630
Nb-090(L00) 2.001980 3.091590 4.411210 5.615940
Nb-090(L02) 0.650844 0.844928 1.259950 1.770700
Nb-091(L00) 26.147500 25.086300 23.985700 22.924400
Nb-091(L01) 10.252800 9.879630 9.569140 9.279330
Nb-092 8.776360 8.634510 8.381030 8.212600
Nb-092(L00) 6.015780 5.877360 5.674670 5.522580
Nb-092(L01) 2.760590 2.757150 2.706360 2.690020
Nb-093(L00) 6.279160 6.786630 7.268600 7.773980
Nb-093(L01) 1.356610 1.417320 1.480530 1.550450
Nb-094(L00) 3.768570 3.925200 4.158320 4.287010
Nb-094(L01) 3.777560 3.852390 4.174570 4.291710
Nb-095 7.411410 7.694960 7.853290 8.242470
Nb-095(L00) 6.969100 7.230100 7.382570 7.753150
Tc-093(L00) 228.672000 230.117000 236.613000 233.893000
Tc-093(L01) 26.800900 27.833400 27.689600 27.083300
Tc-094(L00) 164.440000 154.054000 142.644000 133.287000
Tc-094(L01) 13.786100 13.334300 12.529800 11.707600
Tc-095 100.435000 97.116500 95.291700 95.238100
Tc-095(L00) 86.978500 84.041900 82.558000 82.254100
Tc-095(L01) 13.456400 13.074600 12.733700 12.984000
Tc-096(L00) 20.691900 20.669100 19.987400 18.763300
Tc-096(L01) 7.911620 7.935570 7.660140 6.969670
Tc-097(L00) 0.225346 0.219301 0.214906 0.213265
Zr-090 5.082860 5.704280 5.924980 6.033510
Zr-091 1.068270 1.093200 1.122820 1.142350
答案 0 :(得分:1)
要在单个 DataFrame( df )中查找符合条件的行,可以编写:
df.query('index == "Ru-097" or index.str.startswith("Tc-")')
要汇总这些行,请运行:
df.query('index == "Ru-097" or index.str.startswith("Tc-")')\
.sum(axis=0, skipna=True)
因此整个循环可以采用如下形式:
for i in df_dict:
df = df_dict[i]
xs_elem.loc[i] = df.query('index == "Ru-097" or index.str.startswith("Tc-")')\
.sum(axis=0, skipna=True)
或更短:
for i, df in df_dict.items():
xs_elem.loc[i] = df.query('index == "Ru-097" or index.str.startswith("Tc-")')\
.sum(axis=0, skipna=True)
要再次测试我的代码,我创建了以下DataFrame:
38.0 38.5
product
Mo-093 63.5702 89.7583
Ru-097 29.2205 37.5549
Ru-099 34.0007 52.0004
Tc-094(L00) 877.6860 834.8830
Tc-094(L01) 17.9927 16.9075
Tc-095 86.0709 80.8380
第一个测试是df.query
一个人的工作方式,我得到了:
38.0 38.5
product
Ru-097 29.2205 37.5549
Tc-094(L00) 877.6860 834.8830
Tc-094(L01) 17.9927 16.9075
Tc-095 86.0709 80.8380
然后我测试了整个指令:
df.query('index == "Ru-097" or index.str.startswith("Tc-")')\
.sum(axis=0, skipna=True)
获取:
38.0 1010.9701
38.5 970.1834
dtype: float64
因此,我的代码部分从数据帧中提取所需数据运行正常。 对单个源DataFrame仅执行上述代码,以确认 可以在您的环境中正常工作。
错误的根源可能是您尝试“收集”数据的方式
由df.query(...).sum(...)
在循环的每个循环中找到:
for i in df_dict:
df = df_dict[i]
xs_elem.loc[i] = ...
看看上面循环的最后一行:
xs_elem.loc[i]
返回(可能)返回 Series 的单个元素,
索引== i
,最初,我专注于如何仅从单个DataFrame获取结果, 而不是查看如何/在哪里保存它。
现在我看到这是一个奇怪的构造,导致出现错误。
如何收集数据(我的建议)
运行而不是循环:
xs_elem = pd.DataFrame([ df.query(
'index == "Ru-097" or index.str.startswith("Tc-")')\
.sum(axis=0, skipna=True).rename(key)
for key, df in df_dict.items() ])
详细信息:
for key, df in df_dict.items()
开始阅读,这是一个循环
控制附件列表的理解。
key 包含字典密钥和 df -源dataFrame。df.query(...).sum(...)
生成一个 Series -当前数据
数据框。.rename(key)
。它设置该 Series 的名称,
然后它将是该 Series 系列产生的一行键。