python 从前面的奇数列中减去每个偶数列
python subtract every even column from previous odd column
抱歉,如果之前有人问过这个问题 -- 我找不到这个具体问题。
在python中,我想从前面的奇数列中减去每个偶数列:
所以从:
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113
到
101.849 110.349 68.513
109.95 110.912 61.274
100.612 110.05 62.15
107.75 118.687 59.712
列数未知。我应该在 pandas
还是 numpy
中使用某些东西?
提前致谢。
您可以使用 pandas 完成此操作。您可以 select 分别对偶数和奇数索引列进行减法。
@hiro 主角,我不知道你会做那个 StringIO 魔术。好辣啊
import pandas as pd
import io
data = io.StringIO('''ROI121 ROI122 ROI124 ROI125 ROI126 ROI127
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113''')
df = pd.read_csv(data, sep='\s+')
请注意,even/odd 项可能违反直觉,因为 python 是 0 索引的,这意味着信号列实际上是偶数索引,背景列是奇数索引。如果我正确理解你的问题,这与你使用 even/odd 术语相反。只是指出不同之处以避免混淆。
# strip the columns into their appropriate signal or background groups
bg_df = df.iloc[:, [i for i in range(len(df.columns)) if i%2 == 1]]
signal_df = df.iloc[:, [i for i in range(len(df.columns)) if i%2 == 0]]
# subtract the values of the data frames and store the results in a new data frame
result_df = pd.DataFrame(signal_df.values - bg_df.values)
result_df
包含信号列和背景列之间的差异列。不过,您可能想重命名这些列名称。
>>> result_df
0 1 2
0 101.849 110.349 68.513
1 109.950 110.912 61.274
2 100.612 110.050 62.150
3 107.750 118.687 59.712
import io
# faking the data file
data = io.StringIO('''ROI121 ROI122 ROI124 ROI125 ROI126 ROI127
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113''')
header = next(data) # read the first line from data
# print(header[:-1])
for line in data:
# print(line)
floats = [float(val) for val in line.split()] # create a list of floats
for prev, cur in zip(floats[::2], floats[1::2]):
print('{:6.3f}'.format(prev-cur), end=' ')
print()
输出:
101.849 110.349 68.513
109.950 110.912 61.274
100.612 110.050 62.150
107.750 118.687 59.712
如果您知道 data[start:stop:step]
的含义以及 zip
的工作原理,那么应该很容易理解。
抱歉,如果之前有人问过这个问题 -- 我找不到这个具体问题。
在python中,我想从前面的奇数列中减去每个偶数列:
所以从:
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113
到
101.849 110.349 68.513
109.95 110.912 61.274
100.612 110.05 62.15
107.75 118.687 59.712
列数未知。我应该在 pandas
还是 numpy
中使用某些东西?
提前致谢。
您可以使用 pandas 完成此操作。您可以 select 分别对偶数和奇数索引列进行减法。
@hiro 主角,我不知道你会做那个 StringIO 魔术。好辣啊
import pandas as pd
import io
data = io.StringIO('''ROI121 ROI122 ROI124 ROI125 ROI126 ROI127
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113''')
df = pd.read_csv(data, sep='\s+')
请注意,even/odd 项可能违反直觉,因为 python 是 0 索引的,这意味着信号列实际上是偶数索引,背景列是奇数索引。如果我正确理解你的问题,这与你使用 even/odd 术语相反。只是指出不同之处以避免混淆。
# strip the columns into their appropriate signal or background groups
bg_df = df.iloc[:, [i for i in range(len(df.columns)) if i%2 == 1]]
signal_df = df.iloc[:, [i for i in range(len(df.columns)) if i%2 == 0]]
# subtract the values of the data frames and store the results in a new data frame
result_df = pd.DataFrame(signal_df.values - bg_df.values)
result_df
包含信号列和背景列之间的差异列。不过,您可能想重命名这些列名称。
>>> result_df
0 1 2
0 101.849 110.349 68.513
1 109.950 110.912 61.274
2 100.612 110.050 62.150
3 107.750 118.687 59.712
import io
# faking the data file
data = io.StringIO('''ROI121 ROI122 ROI124 ROI125 ROI126 ROI127
292.087 190.238 299.837 189.488 255.525 187.012
300.837 190.887 299.4 188.488 248.637 187.363
292.212 191.6 299.038 188.988 249.65 187.5
300.15 192.4 307.812 189.125 247.825 188.113''')
header = next(data) # read the first line from data
# print(header[:-1])
for line in data:
# print(line)
floats = [float(val) for val in line.split()] # create a list of floats
for prev, cur in zip(floats[::2], floats[1::2]):
print('{:6.3f}'.format(prev-cur), end=' ')
print()
输出:
101.849 110.349 68.513
109.950 110.912 61.274
100.612 110.050 62.150
107.750 118.687 59.712
如果您知道 data[start:stop:step]
的含义以及 zip
的工作原理,那么应该很容易理解。