簡體   English   中英

有沒有辦法用 pandas、python 或 ZBF57C90277D2BB686D6D65Z3 轉置行頂部的所有 CSV 列值? (第 1 列 = 第 1 行,第 2 列 = 第 2 行)

[英]Is there a way to transpose all CSV column values on top of rows, with pandas, python, or excel? (column 1 = row 1, column 2 = row 2)

我需要獲取 csv 文件的每一列,並將其設置為行,因為 (x,y) 處的每個值在 (y,x) 處都相同。 似乎很容易,但我不斷收到錯誤。 [沿水平方向的單面數據圖像][1] 我嘗試使用 df.iat 和其他幾種 pandas 方法(包括 iloc)進行解析,將列轉換為列表 object,然后嘗試將列表 ZA8CFDE6331BD59EB662AC9C 設置為索引 9 行 146 ,轉置(python 通過 pandas 和 excel 時間)

    # Set to 5 just to verify that it does not erase old data, but adds new
    # true range is 10,000 x 10,000
    for x in range(1,5):
        for y in range(x+1,5):
            a = float(toParse.iat[y,x])
            toParse.iat[x,y] = a

# Data beforehand 
data = {'1': [None,None,None,None,None],
        '2': ['0.6',None,None,None,None],
        '3': ['0.93','1.01',None,None,None],
        '4': ['0.22','0.124','.134',None,None],
        '5': ['0.77','0.012','0.232','0.99',None]}   

# After transposed
data = {'1': [None,'0.6','0.93','0.22','0.77'],
        '2': ['0.6',None,'1.01','0.124','0.012'],
        '3': ['0.93','1.01',None,'0.134','0.232'],
        '4': ['0.22','0.124','0.134',None,'0.99'],
        '5': ['0.77','0.012','0.232','0.99',None]}```  

將其翻轉對角線:

df = pd.DataFrame({
    '1': [None,None,None,None,None],
    '2': ['0.6',None,None,None,None],
    '3': ['0.93','1.01',None,None,None],
    '4': ['0.22','0.124','.134',None,None],
    '5': ['0.77','0.012','0.232','0.99',None]
})

flipped = df.T
flipped.columns = df.columns
flipped.index = df.index

func = lambda col1, col2: col1.combine_first(col2)
df.combine(flipped, func)

原來的:

      1     2     3      4      5
0  None   0.6  0.93   0.22   0.77
1  None  None  1.01  0.124  0.012
2  None  None  None   .134  0.232
3  None  None  None   None   0.99
4  None  None  None   None   None

改造后:

      1      2      3      4      5
0  None    0.6   0.93   0.22   0.77
1   0.6   None   1.01  0.124  0.012
2  0.93   1.01   None   .134  0.232
3  0.22  0.124   .134   None   0.99
4  0.77  0.012  0.232   0.99   None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM