各位高手,以下還是我的作業,我最後一段寫不出來,不會用迴圈增加列和欄標籤數字?
請大家指導~謝謝
##9. 請用 pandas 的功能產生一個內容為 6*8 的隨機變數矩陣,index 為 row_i, columns 為 col_i,其中,index 及 column 須以迴圈+陣列方式產生
import numpy as np
import pandas as pd
import os
pd_data = np. random.rand(6,8)
pd_index=['row_']
pd_col=['col_']
sequences = [1, 2, 3, 4, 5, 6]
for i in sequences:
my_pd= pd.DataFrame (data=pd_data, index=pd_index, columns=pd_col)
path_dst= 'sample folder'
file_name= 'myPD3.csv'
my_pd.to_csv(os.path.join(path_dst, file_name))
import pandas as pd
import numpy as np
pd_data = np.random.rand(6,8)
rowlst = []
for i in range(1, 7):
rowlst.append('row_' + str(i))
collst = []
for i in range(1, 9):
collst.append('col_' + str(i))
my_pd= pd.DataFrame (data=pd_data, index=rowlst, columns=collst)
my_pd
col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8
row_1 0.415260 0.762424 0.142095 0.912391 0.775897 0.136385 0.949409 0.766934
row_2 0.670746 0.082143 0.054060 0.492817 0.595260 0.646294 0.437508 0.423061
row_3 0.164900 0.015406 0.027067 0.853295 0.977336 0.322451 0.669691 0.762756
row_4 0.912144 0.748667 0.623722 0.383255 0.058374 0.227085 0.239988 0.558023
row_5 0.022266 0.623991 0.936121 0.487495 0.213877 0.928989 0.759742 0.753710
row_6 0.760079 0.857624 0.928808 0.927109 0.806327 0.649102 0.984326 0.795047