pandas 数据框添加标题而不替换当前标题 [英] Pandas Dataframe add header without replacing current header

查看:107
本文介绍了 pandas 数据框添加标题而不替换当前标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不替换当前标题的情况下将标题添加到DF?换句话说,我只想向下移动当前标头,然后将其添加到数据帧中作为另一条记录.

How can I add a header to a DF without replacing the current one? In other words I just want to shift the current header down and just add it to the dataframe as another record.

*第二个问题:如何将表(示例数据框)添加到stackoverflow问题?

*secondary question: How do I add tables (example dataframe) to stackoverflow question?

我有这个(注释标题,以及它如何作为一行添加:

I have this (Note header and how it is just added as a row:

   0.213231  0.314544
0 -0.952928 -0.624646
1 -1.020950 -0.883333

我需要这个(将所有其他记录下移并添加新记录) (另:我无法正确读取csv,因为我正在使用s3_text_adapter进行导入,因此我无法弄清楚如何拥有一个类似于pandas read_csv的标头忽略该参数):

I need this (all other records are shifted down and a new record is added) (also: I couldn't read the csv properly because I'm using s3_text_adapter for the import and I couldn't figure out how to have an argument that ignores header similar to pandas read_csv):

       A          B
0  0.213231  0.314544
1 -1.020950 -0.883333

推荐答案

另一个选择是将其添加为列索引的附加级别,以使其成为MultiIndex:

Another option is to add it as an additional level of the column index, to make it a MultiIndex:

In [11]: df = pd.DataFrame(randn(2, 2), columns=['A', 'B'])

In [12]: df
Out[12]: 
          A         B
0 -0.952928 -0.624646
1 -1.020950 -0.883333

In [13]: df.columns = pd.MultiIndex.from_tuples(zip(['AA', 'BB'], df.columns))

In [14]: df
Out[14]: 
         AA        BB
          A         B
0 -0.952928 -0.624646
1 -1.020950 -0.883333

这样做的好处是可以为DataFrame保留正确的dtype,因此您仍然可以在DataFrame上进行快速正确的计算,并且可以通过新旧列名进行访问.

.

为完整起见,这是DSM(已删除的答案),将列排成一行,正如已经提到的,通常不是一个好主意:

For completeness, here's DSM's (deleted answer), making the columns a row, which, as mentioned already, is usually not a good idea:

In [21]: df_bad_idea = df.T.reset_index().T

In [22]: df_bad_idea
Out[22]: 
              0         1
index         A         B
0     -0.952928 -0.624646
1      -1.02095 -0.883333

请注意,在这种情况下,dtype可能会更改(如果这些是列名而不是适当的值)...因此,如果您实际上打算对此做任何工作,请务必小心,因为它可能会变慢,甚至可能失败:

Note, the dtype may change (if these are column names rather than proper values) as in this case... so be careful if you actually plan to do any work on this as it will likely be slower and may even fail:

In [23]: df.sum()
Out[23]: 
A   -1.973878
B   -1.507979
dtype: float64

In [24]: df_bad_idea.sum()  # doh!
Out[24]: Series([], dtype: float64)

如果列名实际上是,则该行被误认为是标题行,那么您在读取数据时应更正此 (例如read_csv使用header=None).

If the column names are actually a row that was mistaken as a header row then you should correct this on reading in the data (e.g. read_csv use header=None).

这篇关于 pandas 数据框添加标题而不替换当前标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆