重置列名并移除转置表的索引。

huangapple go评论63阅读模式
英文:

Reseting Column names and removing Indices of a Transposed table

问题

以下是您要求的翻译部分:

期望的输出是一个水平表格(带有堆叠的表格),左侧有命名的标头,没有/隐藏的索引:

重置列名并移除转置表的索引。

原始数据框:

r1 =

     股票     %变动
0     RELIANCE   (-0.62%)
1     TCS        (-1.68%)
2     HDFCBANK   (-0.77%)
3     ICICIBANK  (-0.72%)

r2 =

     股票        %变动
0     LT         (-0.27%)
1     HCLTECH    (-1.9%)
2     ASIANPAINT (-1.48%)
3     AXISBANK   (-0.41%)

如何重置垂直表格并合并为一个堆叠的水平表格,一个在另一个上面,具有自定义标头名称在左侧,没有/隐藏的索引,以便所有单元格都可以在一个视图中显示(无滚动)?

r_concat只是返回已连接的表格,而不是重命名列。

输入:

r_concat = pd.concat([r1.transpose(),r2.transpose()],axis=0,ignore_index=True).reset_index().rename(columns={'0':'股票','1':'%变动','2':'股票','3':'%变动'}).set_index('股票')

输出:

r_concat =
       0         1           2          3           4          5        6   
0  RELIANCE       TCS    HDFCBANK  ICICIBANK  HINDUNILVR        ITC     INFY   
1  (-1.39%)   (0.03%)    (-0.65%)   (-0.15%)     (0.29%)   (-0.78%)  (0.38%)   
2        LT   HCLTECH  ASIANPAINT   AXISBANK      MARUTI  SUNPHARMA    TITAN   
3  (-0.86%)  (-0.91%)      (0.1%)    (0.01%)    (-1.44%)   (-1.43%)  (0.32%)
英文:

Expected Output is a Horizontal table(with stacked tables) with named headers on the left and no/hidden indices.) :

重置列名并移除转置表的索引。

Original dataframes:
r1 =       
     Stock   %Change
0     RELIANCE  (-0.62%)
1          TCS  (-1.68%)
2     HDFCBANK  (-0.77%)
3    ICICIBANK  (-0.72%)

r2 =        
    Stock   %Change
0           LT  (-0.27%)
1      HCLTECH   (-1.9%)
2   ASIANPAINT  (-1.48%)
3     AXISBANK  (-0.41%)

How to reset vertical tables and merge into a stacked horizontal table one above the other with custom HeaderNames on left and no/hidden Indices so that all the cells can be displayed in one view(No Scroll)?

r_concat simply returns the concatenated table without the renaming columns 

in:    r_concat = pd.concat([r1.transpose(),r2.transpose()],axis=0,ignore_index=True).reset_index().rename(columns={'0':'Stock','1':'%Chg','2':'Stock','3':'%Chg'}).set_index('Stock')
    
out:
    r_concat =   
           0         1           2          3           4          5        6   
    0  RELIANCE       TCS    HDFCBANK  ICICIBANK  HINDUNILVR        ITC     INFY   
    1  (-1.39%)   (0.03%)    (-0.65%)   (-0.15%)     (0.29%)   (-0.78%)  (0.38%)   
    2        LT   HCLTECH  ASIANPAINT   AXISBANK      MARUTI  SUNPHARMA    TITAN   
    3  (-0.86%)  (-0.91%)      (0.1%)    (0.01%)    (-1.44%)   (-1.43%)  (0.32%)

答案1

得分: 0

期望的输出不明确,但您可能想要:
out = pd.DataFrame([r_concat.iloc[1::2].to_numpy().ravel()],
                   columns=r_concat.iloc[::2].to_numpy().ravel()
                   )

输出:

   RELIANCE      TCS  HDFCBANK ICICIBANK HINDUNILVR       ITC     INFY        LT   HCLTECH ASIANPAINT AXISBANK    MARUTI SUNPHARMA    TITAN
0  (-1.39%)  (0.03%)  (-0.65%)  (-0.15%)    (0.29%)  (-0.78%)  (0.38%)  (-0.86%)  (-0.91%)     (0.1%)  (0.01%)  (-1.44%)  (-1.43%)  (0.32%)

或者:


out = pd.DataFrame({'stock': r_concat.iloc[::2].to_numpy().ravel(),
                    'change': r_concat.iloc[1::2].to_numpy().ravel()
                    })

输出:

         stock    change
0     RELIANCE  (-1.39%)
1          TCS   (0.03%)
2     HDFCBANK  (-0.65%)
3    ICICIBANK  (-0.15%)
4   HINDUNILVR   (0.29%)
5          ITC  (-0.78%)
6         INFY   (0.38%)
7           LT  (-0.86%)
8      HCLTECH  (-0.91%)
9   ASIANPAINT    (0.1%)
10    AXISBANK   (0.01%)
11      MARUTI  (-1.44%)
12   SUNPHARMA  (-1.43%)
13       TITAN   (0.32%)

或者:

df = pd.DataFrame.from_dict({'Stock': r_concat.iloc[::2].to_numpy().ravel(),
                             'change': r_concat.iloc[1::2].to_numpy().ravel()},
                            orient='index')

输出:

              0        1         2          3           4         5        6         7         8           9         10        11         12       13
Stock   RELIANCE      TCS  HDFCBANK  ICICIBANK  HINDUNILVR       ITC     INFY        LT   HCLTECH  ASIANPAINT  AXISBANK    MARUTI  SUNPHARMA    TITAN
change  (-1.39%)  (0.03%)  (-0.65%)   (-0.15%)     (0.29%)  (-0.78%)  (0.38%)  (-0.86%)  (-0.91%)      (0.1%)   (0.01%)  (-1.44%)   (-1.43%)  (0.32%)
英文:

The expected output is unclear but you might want:


out = pd.DataFrame([r_concat.iloc[1::2].to_numpy().ravel()],
                   columns=r_concat.iloc[::2].to_numpy().ravel()
                   )

Output:

   RELIANCE      TCS  HDFCBANK ICICIBANK HINDUNILVR       ITC     INFY        LT   HCLTECH ASIANPAINT AXISBANK    MARUTI SUNPHARMA    TITAN
0  (-1.39%)  (0.03%)  (-0.65%)  (-0.15%)    (0.29%)  (-0.78%)  (0.38%)  (-0.86%)  (-0.91%)     (0.1%)  (0.01%)  (-1.44%)  (-1.43%)  (0.32%)

Or maybe:


out = pd.DataFrame({'stock': r_concat.iloc[::2].to_numpy().ravel(),
                    'change': r_concat.iloc[1::2].to_numpy().ravel()
                    })

Output:

         stock    change
0     RELIANCE  (-1.39%)
1          TCS   (0.03%)
2     HDFCBANK  (-0.65%)
3    ICICIBANK  (-0.15%)
4   HINDUNILVR   (0.29%)
5          ITC  (-0.78%)
6         INFY   (0.38%)
7           LT  (-0.86%)
8      HCLTECH  (-0.91%)
9   ASIANPAINT    (0.1%)
10    AXISBANK   (0.01%)
11      MARUTI  (-1.44%)
12   SUNPHARMA  (-1.43%)
13       TITAN   (0.32%)

Or:

df = pd.DataFrame.from_dict({'Stock': r_concat.iloc[::2].to_numpy().ravel(),
                             'change': r_concat.iloc[1::2].to_numpy().ravel()},
                            orient='index')

Output:

              0        1         2          3           4         5        6         7         8           9         10        11         12       13
Stock   RELIANCE      TCS  HDFCBANK  ICICIBANK  HINDUNILVR       ITC     INFY        LT   HCLTECH  ASIANPAINT  AXISBANK    MARUTI  SUNPHARMA    TITAN
change  (-1.39%)  (0.03%)  (-0.65%)   (-0.15%)     (0.29%)  (-0.78%)  (0.38%)  (-0.86%)  (-0.91%)      (0.1%)   (0.01%)  (-1.44%)   (-1.43%)  (0.32%)

答案2

得分: 0

使用:

r_concat = pd.concat([r1.T,r2.T])

或者:

r_concat = pd.concat([r1,r2], axis=1).T

print (r_concat)
              0         1           2          3
Stock    RELIANCE       TCS    HDFCBANK  ICICIBANK
%Change  (-0.62%)  (-1.68%)    (-0.77%)   (-0.72%)
Stock          LT   HCLTECH  ASIANPAINT   AXISBANK
%Change  (-0.27%)   (-1.9%)    (-1.48%)   (-0.41%)

要写入文件或在不显示列名的情况下显示,请使用:

r_concat.to_csv('filename.csv', header=False)
print (r_concat.to_string(header=False))
英文:

Use:

r_concat = pd.concat([r1.T,r2.T])

Or:

r_concat = pd.concat([r1,r2], axis=1).T

print (r_concat)
                0         1           2          3
Stock    RELIANCE       TCS    HDFCBANK  ICICIBANK
%Change  (-0.62%)  (-1.68%)    (-0.77%)   (-0.72%)
Stock          LT   HCLTECH  ASIANPAINT   AXISBANK
%Change  (-0.27%)   (-1.9%)    (-1.48%)   (-0.41%)

For write to file or display without columns names use:

r_concat.to_csv('filename.csv', header=False)

print (r_concat.to_string(header=False))

答案3

得分: 0

以下是已翻译的内容:

Used Join & Transpose and got the expected Horizontal table(with stacked tables) with named headers on the left and no/hidden indices.

In:

  r_join = r1.join(r2,lsuffix='_0', rsuffix='_1')
    r_join =r_join.reset_index().set_index('Stock_0').drop(['index'], axis=1).transpose()

Out:

 r_join =
     Stock_0    RELIANCE       TCS    HDFCBANK ICICIBANK HINDUNILVR        ITC 
    %Change_0  (-0.55%)  (-1.75%)    (-1.21%)  (-0.73%)   (-0.57%)   (-0.02%)   
    Stock_1          LT   HCLTECH  ASIANPAINT  AXISBANK     MARUTI  SUNPHARMA   
    %Change_1  (-0.48%)   (-1.9%)    (-1.84%)  (-0.54%)    (0.04%)   (-1.16%)
英文:

Used Join & Transpose and got the expected Horizontal table(with stacked tables) with named headers on the left and no/hidden indices.

In:

  r_join = r1.join(r2,lsuffix='_0', rsuffix='_1')
    r_join =r_join.reset_index().set_index('Stock_0').drop(['index'], axis=1).transpose()

Out:

 r_join =
     Stock_0    RELIANCE       TCS    HDFCBANK ICICIBANK HINDUNILVR        ITC 
    %Change_0  (-0.55%)  (-1.75%)    (-1.21%)  (-0.73%)   (-0.57%)   (-0.02%)   
    Stock_1          LT   HCLTECH  ASIANPAINT  AXISBANK     MARUTI  SUNPHARMA   
    %Change_1  (-0.48%)   (-1.9%)    (-1.84%)  (-0.54%)    (0.04%)   (-1.16%)

huangapple
  • 本文由 发表于 2023年5月17日 10:28:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定