numpy 将Pandas列转换为Pandas行

von4xj4u  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(140)

我需要你的帮助。我有三列在Pandas数据框1,这是(X,Y,Z)和每列包含100K值。
我想创建一个新的Pandas数据框,列(X0,X1,...X499,Y0,Y1,Y499,Z0,Z1,Z499)总共1500列,第二个数据框行从第一个数据框行取值
我如何在Python中执行此操作

data1 =pd.read_excel('/content/sattle1.2.xlsx')
X_np = data1['X'].to_numpy()
Y_np =data1['Y'].to_numpy()
Z_np =data1['Z'].to_numpy()
i = 0
for i in range(len(X_np)):
  # append 500 of x, y, and z 
  row = np.concatenate(   X_np[0:500], Y_np[0:500] ,     Z_np[0:500]    )
  row = pd.DataFrame(row).T
  # append this row to the result DataFrame
  result1 = pd.concat([result1, row], ignore_index=True)
6mzjoqzu

6mzjoqzu1#

下面是一个例子:

import pandas as pd
import numpy as np

data = {
    'X': np.random.randn(100000),
    'Y': np.random.randn(100000),
    'Z': np.random.randn(100000)
}
df = pd.DataFrame(data)

X_reshaped = df['X'].values.reshape(-1, 500)
Y_reshaped = df['Y'].values.reshape(-1, 500)
Z_reshaped = df['Z'].values.reshape(-1, 500)

new_data = np.concatenate((X_reshaped, Y_reshaped, Z_reshaped), axis=1)

new_df = pd.DataFrame(new_data, columns=[f'{col}{i}' for col in 'XYZ' for i in range(500)])

print('Original DataFrame:')
print(df)
print('New DataFrame:')
print(new_df)

它返回

Original DataFrame:
              X         Y         Z
0     -0.140111 -1.747893  0.075925
1      0.051040 -0.717775  0.193122
2     -0.152391 -0.890001 -0.916481
3      0.657520  1.378301 -0.587179
4      0.115638  0.620887 -0.488266
...         ...       ...       ...
99995  1.026177 -0.829365 -0.381241
99996 -1.247452  0.421909  0.532482
99997 -0.802279 -0.765697  0.152494
99998 -2.240205 -0.643563 -1.468143
99999  0.472148 -0.000043  0.086280

[100000 rows x 3 columns]
New DataFrame:
           X0        X1        X2        X3        X4        X5        X6  \
0   -0.140111  0.051040 -0.152391  0.657520  0.115638  0.065367 -1.642060   
1   -0.369551  1.579626  0.726445 -0.111039 -1.413619  0.838366  1.651923   
2    1.561334  0.263964 -2.177334 -1.393858  1.018796 -0.052691  0.623450   
3   -0.875493  1.991197  0.317792  1.272978 -0.512567  1.193433 -1.183477   
4   -0.246873  0.250776 -0.202831  0.031202  0.063812  1.239713  0.398995   
..        ...       ...       ...       ...       ...       ...       ...   
195  0.197690  0.226558 -0.342978 -0.673168 -1.559666 -1.024200  0.572472   
196 -0.112484 -0.046911 -0.459250  0.328672  1.256746 -0.102448  0.037744   
197  0.664545 -0.554257  0.626517  0.048981 -1.688024 -2.023502 -0.550704   
198 -0.796657 -2.767073 -0.339256  0.169755  0.455841 -0.813645  0.612488   
199 -0.269652 -0.005751 -1.391671  0.435838 -0.892750  0.879295 -1.026426   

           X7        X8        X9  ...      Z490      Z491      Z492  \
0    0.512578  1.754822  0.485309  ... -3.856192 -1.208035  0.722118   
1   -0.462331  0.838469 -1.175573  ...  0.077797 -0.841695 -0.932464   
2   -0.506552  0.686787 -2.520865  ...  0.322746  1.316753 -1.873400   
3   -1.012044  1.031499  0.288520  ...  1.631240  0.354918  1.090476   
4    0.702607 -0.891292 -0.081734  ... -1.918829 -0.802564 -2.270525   
..        ...       ...       ...  ...       ...       ...       ...   
195 -0.365689  0.298325  0.770454  ...  0.304790  0.285013  0.283750   
196 -0.308815  0.665268 -0.019363  ... -0.047142  0.840220 -1.387166   
197  0.171641  1.035029  1.986789  ... -0.474015 -0.271134 -0.007936   
198  1.355988  0.419378  1.109493  ...  0.361771  0.680821  0.976546   
199 -0.339775  0.508457 -1.513810  ...  0.926244  1.000737  0.304316   

         Z493      Z494      Z495      Z496      Z497      Z498      Z499  
0   -1.233024 -1.504768 -0.829607 -0.626522  1.786107  0.432834 -0.041990  
1    0.197283  0.311848 -2.074356  0.138292  0.351503  0.128665 -1.045443  
2    0.344700  0.577543  0.198043 -1.065644 -0.636203 -0.192781  2.243618  
3    0.058017 -0.189505 -0.217413 -0.821730 -1.212244  1.420549  0.205270  
4    0.616337 -0.229167 -0.301775  0.644389  0.074468 -1.320099 -0.627609  
..        ...       ...       ...       ...       ...       ...       ...  
195  0.767952  0.613438  0.924020  1.189010 -0.664778  0.395277 -0.008522  
196 -1.344793 -0.643846  1.213020  0.478520 -1.163467 -0.586328  0.034092  
197 -0.459195  0.084574  1.185811 -0.329050 -1.068876  0.446618 -0.391414  
198  0.907331 -1.685934  2.649564  0.366982 -1.575702 -0.500473 -1.531845  
199 -0.102133  0.066453 -0.381241  0.532482  0.152494 -1.468143  0.086280  

[200 rows x 1500 columns]
´``

相关问题