【发布时间】:2021-12-28 23:59:51
【问题描述】:
我正在尝试重新排列 csv 文件。该文件是这样设置的。我将 Python 3 与 numpy 和 pandas 库一起使用。
| Element | Temperature | Fy |
|---|---|---|
| Element A | 27 | 320 |
| Element A | 100 | 310 |
| Element A | 200 | 300 |
| Element A | 300 | 290 |
| Element A | 400 | 280 |
| Element B | 27 | 320 |
| Element B | 100 | 310 |
| Element B | 200 | 300 |
| Element B | 350 | 290 |
| Element B | 400 | 280 |
第 1 列有元素名称,第 2 列有温度,第 3 列有相应的屈服应力。需要注意的是,某些元素可能包含不同的温度值。我需要重新组织列以水平运行。
| Element | 27 | 100 | 200 | 300 | 350 | 400 |
|---|---|---|---|---|---|---|
| Element A | 320 | 310 | 300 | 290 | 0 | 280 |
| Element B | 320 | 310 | 300 | 0 | 290 | 280 |
到目前为止,我的代码是这样的:
df = pd.read_csv('MatNavi.csv', header=0)
Elements = df['Element'].unique() # Creates a numpy array of all elements, no duplicates, This will become my first column in the final csv file
X = df['Temperature'].drop(Mn[Mn['Element'] != 'Element A'].index) #creates a numpy array with the temperature values then drops all temperature values that are not for Element A
Y = df['Fy'].drop(Mn[Mn['Element'] != 'Element A'].index) #same thing but for Fy
这就是我卡住的地方。我需要创建一个列表、数组或数据框,将 X 值(温度)作为列标题,然后将 Fy 值水平放置在其下方,并使其与元素 A 行标题相对应。我假设温度值应该是指数(不确定这是否是正确的术语),然后 pandas 或 numpy 可以用来按顺序对 Fy 值进行分类。我只是不知道如何开始。
【问题讨论】: