pandas 将 Dataframe 的特定列移动到第一列

kgqe7b3p  于 2022-12-02  发布在  其他
关注(0)|答案(3)|浏览(258)

我有一个如下的 Dataframe :

s = df.head().to_dict()
print(s)

{'BoP transfers': {1998: 12.346282212735618,
  1999: 19.06438060024298,
  2000: 18.24888031473687,
  2001: 24.860019912667006,
  2002: 32.38242225822908},
 'Current balance': {1998: -6.7953,
  1999: -2.9895,
  2000: -3.9694,
  2001: 1.1716,
  2002: 5.7433},
 'Domestic demand': {1998: 106.8610389799729,
  1999: 104.70302507466538,
  2000: 104.59254229534136,
  2001: 103.83532232336977,
  2002: 102.81709401489702},
 'Effective exchange rate': {1998: 88.134,
  1999: 95.6425,
  2000: 99.927725,
  2001: 101.92745,
  2002: 107.85565},
 'RoR (foreign liabilities)': {1998: 0.0433,
  1999: 0.0437,
  2000: 0.0542,
  2001: 0.0539,
  2002: 0.0474},
 'Gross foreign assets': {1998: 19.720897432405103,
  1999: 22.66200738564236,
  2000: 25.18270679890144,
  2001: 30.394226651732836,
  2002: 37.26477320359688},
 'Gross domestic income': {1998: 104.9037939043707,
  1999: 103.15361867816479,
  2000: 103.06777792080423,
  2001: 102.85886528974339,
  2002: 102.28518242008846},
 'Gross foreign liabilities': {1998: 60.59784839338306,
  1999: 61.03308220978983,
  2000: 64.01438055825233,
  2001: 67.07798172469921,
  2002: 70.16108592109364},
 'Inflation rate': {1998: 52.6613,
  1999: 19.3349,
  2000: 16.0798,
  2001: 15.076,
  2002: 17.236},
 'Credit': {1998: 0.20269913592846378,
  1999: 0.2154280880177353,
  2000: 0.282948948505006,
  2001: 0.3954812893893278,
  2002: 0.3578263032373988}}

可以使用以下方法将其转换回其原始形式:

df = pd.DataFrame.from_dict(s)

假设,我想把名为“Gross foreign liabilities”的列移到第一列。我知道这可以通过重新索引来完成。但是,在我的例子中, Dataframe 有100列。我如何才能把一个特定的列移到最开始呢?我读过panda pop()方法,但是在我的框架中,我得到了一个错误。

efzxgjgh

efzxgjgh1#

以下是我的几种方法:

columns = df.columns.tolist()
columns.insert(0, columns.pop(columns.index("Gross foreign liabilities")))
df = df.reindex(columns=columns)

col = ["Gross foreign liabilities"]
df = df[col + [x for x in df.columns if x not in col]]
2guxujil

2guxujil2#

您可以使用popinsert

name = 'Gross foreign liabilities'
df.insert(0, name, df.pop(name))

作为函数:

def move_first(df, name):
    df.insert(0, name, df.pop(name))

move_first(df, 'Gross foreign liabilities')

输出量:

Gross foreign liabilities  BoP transfers  Current balance  \
1998                  60.597848      12.346282          -6.7953   
1999                  61.033082      19.064381          -2.9895   
2000                  64.014381      18.248880          -3.9694   
2001                  67.077982      24.860020           1.1716   
2002                  70.161086      32.382422           5.7433   

      Domestic demand  Effective exchange rate  RoR (foreign liabilities)  \
1998       106.861039                88.134000                     0.0433   
1999       104.703025                95.642500                     0.0437   
2000       104.592542                99.927725                     0.0542   
2001       103.835322               101.927450                     0.0539   
2002       102.817094               107.855650                     0.0474   

      Gross foreign assets  Gross domestic income  Inflation rate    Credit  
1998             19.720897             104.903794         52.6613  0.202699  
1999             22.662007             103.153619         19.3349  0.215428  
2000             25.182707             103.067778         16.0798  0.282949  
2001             30.394227             102.858865         15.0760  0.395481  
2002             37.264773             102.285182         17.2360  0.357826
mutmk8jj

mutmk8jj3#

示例

data = {'col1': {0: 0, 1: 2}, 'col2': {0: 1, 1: 3}, 'col3': {0: 2, 1: 4}}
df = pd.DataFrame(data)

df

col1    col2    col3
0   0       1       2
1   2       3       4

代码

df.insert(0, 'col3', df.pop('col3'))

输出(df):

col3    col1    col2
0   2       0       1
1   4       2       3

使示例更简单plz

相关问题