【问题标题】:How do I change the original dataframe column name to a new name?如何将原始数据框列名称更改为新名称?
【发布时间】:2021-03-29 22:01:03
【问题描述】:

以下数据是根据一些用户输入提取的。

  1. 如何将索引更改为从第 1 个 iso 列 ID 开始。编号列是新的。
  2. 如何更改列名并仅选择
import pandas as pd
df = pd.read_csv("past_transacted_px.csv")

** user input field programme **
       
print('The', desired_flat, 'flats available in', town_choice, 'are:')
        
flat_list = df.loc[(df['town'] == town_choice) & (df['flat_type'] == desired_flat) & (df['resale_price'] >= min_price) & (df['resale_price'] <= max_price)]
columns = ['No.','block', 'street_name', 'storey_range','floor_area_sqm','resale_price']
df_renamed = df.rename(columns={"block":"Block", "street_name":"Street Name","storey_range":"Storey Range","floor_area_sqm":"Size (sq m)","resale_price":"Price($)"})

df_renamed = pd.DataFrame(flat_list, columns = columns)
print(df_renamed)
This is the current output I get

The EXECUTIVE flats available in A are:
         No. block     street_name storey_range  floor_area_sqm  resale_price
2259    2260   391               A     07 TO 09           102.0      150000.0
2260    2261   391               A     07 TO 09            92.0      250000.0
9732    9733   406               A     04 TO 06           195.0      150000.0
13472  13473   351               A     01 TO 03           106.0      230000.0

I need the output to be like this


The EXECUTIVE flats available in A are:
**No. Block     Street Name Storey Range  Floor Area Sqm  Resale Price**
**1**   391               A     07 TO 09           **102      150,000**
**2**   391               A     07 TO 09            **92      250,000**
**3**   406               A     04 TO 06           **195      150,000**
**4**   351               A     01 TO 03           **106      230,000**


【问题讨论】:

    标签: python pandas dataframe rename new-operator


    【解决方案1】:

    这可能会解决您的问题:

    df.drop(df.columns[i], axis=1)
    

    【讨论】:

      【解决方案2】:

      重命名列后,您将使用定义的旧列列表,从而解决列名错误。

      df_renamed = pd.DataFrame(flat_list, columns = columns)

      应该删除

      打印(df_renamed)

      【讨论】:

        【解决方案3】:

        我已设法将列名更改为正确的格式。

        print('The', desired_flat, 'flats available in', town_choice, 'are:')
                        
        flat_list = df.loc[(df['town'] == town_choice) & (df['flat_type'] == desired_flat) & (df['resale_price'] >= min_price) & (df['resale_price'] <= max_price)]
        df_cols = ['month','town','flat_type','Block', 'Street Name', 'Storey Range','Size (sq m)','flat_model', 'lease_commence_date', 'remaining_lease','Price($)']
        flat_list.columns = df_cols
                
        print(flat_list[['Block', 'Street Name', 'Storey Range','Size (sq m)', 'Price($)']])
        

        但是我仍然无法添加新的索引号,请删除原始的 excel 列号。引用并格式化 Sqm 和 Value 的小数位。

        【讨论】:

          猜你喜欢
          • 2021-02-15
          • 1970-01-01
          • 2019-04-28
          • 2015-09-22
          • 1970-01-01
          • 2021-07-02
          • 2016-10-17
          • 2018-05-09
          • 2020-04-10
          相关资源
          最近更新 更多