gpt4 book ai didi

python - 使用pandas存储实验数据

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:59 24 4
gpt4 key购买 nike

我正在使用 pandas DataFrame 来存储来自一系列实验的数据,以便我可以轻松地为下一阶段的分析截取各种参数值。我有几个关于如何最有效地做到这一点的问题。

目前我从列表字典创建我的 DataFrame。 DataFrame 中通常有几千行。其中一列是 device_id,它指示实验数据属于 20 个设备中的哪一个。其他列包括有关实验设置的信息,如温度、功率等,以及测量结果,如谐振频率、带宽等。

到目前为止,我一直在相当“天真地”使用这个 DataFrame,也就是说,我使用它有点像 numpy 记录数组,所以我认为我没有充分利用数据框。以下是我正在努力实现的一些示例。

首先我想创建一个新列,它是在所有实验中为给定设备测量的最大谐振频率:将其称为 max_freq。我这样做是这样的:

df['max_freq'] = np.zeros((data.shape[0])) #  create the new column
for index in np.unique(df.device_index):
group = df[df.device_index == index]
max = group.resonant_frequency.max()
df.max_freq[df.resonator_index == index] = max

第二 我的一个专栏包含噪声测量的一维 numpy 数组。我想计算这个一维数组的统计数据并将其放入一个新列中。目前我这样做是:

noise_est = []
for vals,freq in (df.noise,df.resonant_freq):
noise_est.append(vals.std()/(1e6*freq))
df['noise_est'] = noise_est

第三 与上一个相关:是否可以遍历 DataFrame 的行,其中生成的对象具有对列的属性访问权限? IE。像这样的东西:

for row in df:
row.noise_est = row.noise.std()/(1e6*row.resonant_freq)

我知道这会遍历列。我也知道有一个 iterrows 方法,但这提供了一个不允许属性访问的系列。

我认为这应该让我现在开始,感谢您抽出时间!

根据要求编辑添加 df.info()、df.head():

df.info() # df.head() looks the same, but 5 non-null values

<class 'pandas.core.frame.DataFrame'>
Int64Index: 9620 entries, 0 to 9619
Data columns (total 83 columns):
A_mag 9620 non-null values
A_mag_err 9620 non-null values
A_phase 9620 non-null values
A_phase_err 9620 non-null values
....
total_dac_atten 9600 non-null values
round_temp 9620 non-null values
dtypes: bool(1), complex128(4), float64(39), int64(12), object(27)

我缩减了它,因为它有 83 列,我认为这不会对我共享的示例代码片段增加太多,但已发布这一点以防它有帮助。

最佳答案

创建数据。请注意,在框架内存储一个 numpy 数组通常不是一个好主意,因为它非常低效。

In [84]: df = pd.DataFrame(dict(A = np.random.randn(20), B = np.random.randint(0,3,size=20), C = [ np.random.randn(5) for i in range(20) ]))

In [85]: df
Out[85]:
A B C
0 -0.493730 1 [-0.8790126045, -1.87366673214, 0.76227570837,...
1 -0.105616 2 [0.612075134682, -1.64452324091, 0.89799758012...
2 1.487656 1 [-0.379505426885, 1.17611806172, 0.88321152932...
3 0.351694 2 [0.132071242514, -1.54701609348, 1.29813626801...
4 -0.330538 2 [0.395383858214, 0.874419943107, 1.21124463921...
5 0.360041 0 [0.439133138619, -1.98615530266, 0.55971723554...
6 -0.505198 2 [-0.770830608002, 0.243255072359, -1.099514797...
7 0.631488 1 [0.676233200011, 0.622926691271, -0.1110029751...
8 1.292087 1 [1.77633938532, -0.141683361957, 0.46972952154...
9 0.641987 0 [1.24802709304, 0.477527098462, -0.08751885691...
10 0.732596 2 [0.475771915314, 1.24219702097, -0.54304296895...
11 0.987054 1 [-0.879620967644, 0.657193159735, -0.093519342...
12 -1.409455 1 [1.04404325784, -0.310849157425, 0.60610368623...
13 1.063830 1 [-0.760467872808, 1.33659372288, -0.9343171844...
14 0.533835 1 [0.985463451645, 1.76471927635, -0.59160181340...
15 0.062441 1 [-0.340170594584, 1.53196133354, 0.42397775978...
16 1.458491 2 [-1.79810090668, -1.82865815817, 1.08140831482...
17 -0.886119 2 [0.281341969073, -1.3516126536, 0.775326038501...
18 0.662076 1 [1.03992509625, 1.17661862104, -0.562683934951...
19 1.216878 2 [0.0746149754367, 0.156470450639, -0.477269150...

In [86]: df.dtypes
Out[86]:
A float64
B int64
C object
dtype: object

对系列(2 和 3)的值应用操作

In [88]: df['C_std'] = df['C'].apply(np.std)

获取每组的最大值并返回值(1)

In [91]: df['A_max_by_group'] = df.groupby('B')['A'].transform(lambda x: x.max())

In [92]: df
Out[92]:
A B C A_max_by_group C_std
0 -0.493730 1 [-0.8790126045, -1.87366673214, 0.76227570837,... 1.487656 1.058323
1 -0.105616 2 [0.612075134682, -1.64452324091, 0.89799758012... 1.458491 0.987980
2 1.487656 1 [-0.379505426885, 1.17611806172, 0.88321152932... 1.487656 1.264522
3 0.351694 2 [0.132071242514, -1.54701609348, 1.29813626801... 1.458491 1.150026
4 -0.330538 2 [0.395383858214, 0.874419943107, 1.21124463921... 1.458491 1.045408
5 0.360041 0 [0.439133138619, -1.98615530266, 0.55971723554... 0.641987 1.355853
6 -0.505198 2 [-0.770830608002, 0.243255072359, -1.099514797... 1.458491 0.443872
7 0.631488 1 [0.676233200011, 0.622926691271, -0.1110029751... 1.487656 0.432342
8 1.292087 1 [1.77633938532, -0.141683361957, 0.46972952154... 1.487656 1.021847
9 0.641987 0 [1.24802709304, 0.477527098462, -0.08751885691... 0.641987 0.676835
10 0.732596 2 [0.475771915314, 1.24219702097, -0.54304296895... 1.458491 0.857441
11 0.987054 1 [-0.879620967644, 0.657193159735, -0.093519342... 1.487656 0.628655
12 -1.409455 1 [1.04404325784, -0.310849157425, 0.60610368623... 1.487656 0.835633
13 1.063830 1 [-0.760467872808, 1.33659372288, -0.9343171844... 1.487656 0.936746
14 0.533835 1 [0.985463451645, 1.76471927635, -0.59160181340... 1.487656 0.991327
15 0.062441 1 [-0.340170594584, 1.53196133354, 0.42397775978... 1.487656 0.700299
16 1.458491 2 [-1.79810090668, -1.82865815817, 1.08140831482... 1.458491 1.649771
17 -0.886119 2 [0.281341969073, -1.3516126536, 0.775326038501... 1.458491 0.910355
18 0.662076 1 [1.03992509625, 1.17661862104, -0.562683934951... 1.487656 0.666237
19 1.216878 2 [0.0746149754367, 0.156470450639, -0.477269150... 1.458491 0.275065

关于python - 使用pandas存储实验数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23768963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com