gpt4 book ai didi

python - 如何对具有重复列名的行进行切片并按顺序堆叠这些行

转载 作者:行者123 更新时间:2023-12-01 12:04:29 25 4
gpt4 key购买 nike

enter image description here

我有一个如图所示的数据框,我想在不更改顺序的情况下将其转换为多行。

  RESP HR SPO2 PULSE
1 46 122 0 0
2 46 122 0 0
3
4

最佳答案

一种可能的解决方案是使用 reshape , 列长度的唯一必要模数是 0 (因此可以将所有数据转换为 4 列 DataFrame):

df1 = pd.Dataframe(df.values.reshape(-1, 4), columns=['RESP','HR','SPO2','PULSE'])
df1['RESP1'] = df['RESP'].shift(-1)

通用数据解决方案:

a = '46 122 0 0 46 122 0 0 45 122 0 0 45 122 0'.split()
df = pd.DataFrame([a]).astype(int)
print (df)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 46 122 0 0 46 122 0 0 45 122 0 0 45 122 0

#flatten values
a = df.values.ravel()
#number of new columns
N = 4
#array filled by NaNs for possible add NaNs to end of last row
arr = np.full(((len(a) - 1)//N + 1)*N, np.nan)
#fill array by flatten values
arr[:len(a)] = a
#reshape to new DataFrame (last value is NaN)
df1 = pd.DataFrame(arr.reshape((-1, N)), columns=['RESP','HR','SPO2','PULSE'])
#new column with shifting first col
df1['RESP1'] = df1['RESP'].shift(-1)
print(df1)
RESP HR SPO2 PULSE RESP1
0 46.0 122.0 0.0 0.0 46.0
1 46.0 122.0 0.0 0.0 45.0
2 45.0 122.0 0.0 0.0 45.0
3 45.0 122.0 0.0 NaN NaN

关于python - 如何对具有重复列名的行进行切片并按顺序堆叠这些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105439/

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