动态 - 自动乘法 - Pandas 数据帧
Dynamic - Automated multiplication - Pandas dataframes
在 Whosebug 和网络上花了很长时间搜索和阅读后,我感到绝望...
我有一个 Pandas DataFrame,其中包含一些导入的数据(光谱)。第一列是波长,而其他列是各种光谱(数据)。列的名称是从一个列表中导入的,该列表从路径中读取文件名并仅保留名称。
我想要实现但似乎不太明白的是如何将每一列与波长列相乘并覆盖现有的列或创建一个新的数据帧(没那么重要).
这是我目前使用的代码(即使不是最优雅的代码,也能完成工作):
path = r'"thePathToData\PL_calc\Data_NIR'
idx = 0
#Create the DataFrame with all the data from the path above, use the filenames as column names
all_files = glob.glob(os.path.join(path, "*.asc"))
df = pd.concat((pd.read_csv(f, usecols=[1], sep='\t') for f in all_files), axis=1) #usecol=1 for the spectrum only
fileNames = [] # create a list for the filenames
for i in range(0,len(all_files)):
fileNames.append(all_files[i][71:-4])
df.columns = fileNames # assign the filenames as columns
wavelengths = pd.read_csv(all_files[0], usecols=[0], sep='\t') # add the wavelength column as first column of the dataframe
df.insert(loc=idx, column='Wavelength', value=wavelengths)
如果我只绘制 DF 的头部,它看起来像这样:
Wavelength F8BT_Pure_Batch1_px1_spectra_4V \ ...
0 478.0708 -3.384101
1 478.3917 -1.580399
2 478.7126 -0.323580
3 479.0334 -1.131425
4 479.3542 1.202728
完整的DF是:
1599 rows × 46 columns
问题一:
我找不到一种自动(动态)方式将每个列与第一个列相乘,基本上是这样的:
for i in range(1, len(df.columns)):
df[[i]] = df[[0]] * df[[i]]
问题二:
为什么这样做:
df['F8BT_Pure_Batch1_px1_spectra_4V'] = df['Wavelength']*df['F8BT_Pure_Batch1_px1_spectra_4V']
虽然这没有,但给了我一个 "IndexError: indices are out-of-bounds"
df[[1]] = df[[0]]*df[[1]]
但是当我 print(df[['Wavelength']]) Name: Wavelength, dtype: float64
和 print(df[[0]]) [1599 rows x 1 columns]
我得到相同的数字..
问题三:
为什么这个df[fileNames] = df[fileNames].multiply(df.Wavelength)
给我一个ValueError: Columns must be same length as key
?所有列的长度都相同(1599 行长,0-1598,在这种情况下总共有 46 列)。 fileNames
包含导入文件的名称和数据框列的名称。
非常感谢您的帮助...
亚历克斯
问题 1
要将您的波长列乘以 DataFrame 中的所有其他列,您可以使用:
df.iloc[:, 1:] = df.iloc[:, 1:].mul(df['Wavelength'], axis=0)
这假设您的波长列是第一列。
问题二
使用整数选择像这样的列要求您的 DataFrame 中名为 0、1 等的列作为整数。您的 DataFrame 中有 none。要按索引号查看 select 列,请查看 pandas' iloc method.
的文档
问题 3
当您调用 df[fileNames]
时,您将获得一个列数与列表长度相同的 DataFrame fileNames
。您的代码 df[fileNames].multiply(df.Wavelength)
没有为您提供列数与 df[fileNames]
相同的 DataFrame,因此您无法分配值。在乘法函数中使用 axis=0
参数对我有用。
在 Whosebug 和网络上花了很长时间搜索和阅读后,我感到绝望...
我有一个 Pandas DataFrame,其中包含一些导入的数据(光谱)。第一列是波长,而其他列是各种光谱(数据)。列的名称是从一个列表中导入的,该列表从路径中读取文件名并仅保留名称。
我想要实现但似乎不太明白的是如何将每一列与波长列相乘并覆盖现有的列或创建一个新的数据帧(没那么重要).
这是我目前使用的代码(即使不是最优雅的代码,也能完成工作):
path = r'"thePathToData\PL_calc\Data_NIR'
idx = 0
#Create the DataFrame with all the data from the path above, use the filenames as column names
all_files = glob.glob(os.path.join(path, "*.asc"))
df = pd.concat((pd.read_csv(f, usecols=[1], sep='\t') for f in all_files), axis=1) #usecol=1 for the spectrum only
fileNames = [] # create a list for the filenames
for i in range(0,len(all_files)):
fileNames.append(all_files[i][71:-4])
df.columns = fileNames # assign the filenames as columns
wavelengths = pd.read_csv(all_files[0], usecols=[0], sep='\t') # add the wavelength column as first column of the dataframe
df.insert(loc=idx, column='Wavelength', value=wavelengths)
如果我只绘制 DF 的头部,它看起来像这样:
Wavelength F8BT_Pure_Batch1_px1_spectra_4V \ ...
0 478.0708 -3.384101
1 478.3917 -1.580399
2 478.7126 -0.323580
3 479.0334 -1.131425
4 479.3542 1.202728
完整的DF是:
1599 rows × 46 columns
问题一:
我找不到一种自动(动态)方式将每个列与第一个列相乘,基本上是这样的:
for i in range(1, len(df.columns)):
df[[i]] = df[[0]] * df[[i]]
问题二:
为什么这样做:
df['F8BT_Pure_Batch1_px1_spectra_4V'] = df['Wavelength']*df['F8BT_Pure_Batch1_px1_spectra_4V']
虽然这没有,但给了我一个 "IndexError: indices are out-of-bounds"
df[[1]] = df[[0]]*df[[1]]
但是当我 print(df[['Wavelength']]) Name: Wavelength, dtype: float64
和 print(df[[0]]) [1599 rows x 1 columns]
我得到相同的数字..
问题三:
为什么这个df[fileNames] = df[fileNames].multiply(df.Wavelength)
给我一个ValueError: Columns must be same length as key
?所有列的长度都相同(1599 行长,0-1598,在这种情况下总共有 46 列)。 fileNames
包含导入文件的名称和数据框列的名称。
非常感谢您的帮助...
亚历克斯
问题 1
要将您的波长列乘以 DataFrame 中的所有其他列,您可以使用:
df.iloc[:, 1:] = df.iloc[:, 1:].mul(df['Wavelength'], axis=0)
这假设您的波长列是第一列。
问题二
使用整数选择像这样的列要求您的 DataFrame 中名为 0、1 等的列作为整数。您的 DataFrame 中有 none。要按索引号查看 select 列,请查看 pandas' iloc method.
的文档问题 3
当您调用 df[fileNames]
时,您将获得一个列数与列表长度相同的 DataFrame fileNames
。您的代码 df[fileNames].multiply(df.Wavelength)
没有为您提供列数与 df[fileNames]
相同的 DataFrame,因此您无法分配值。在乘法函数中使用 axis=0
参数对我有用。