简体   繁体   中英

How do I change the original dataframe column name to a new name?

The following data was extracted based on a few user inputs.

  1. How do I change the index to start from No. 1 iso the column ID. No. column is new.
  2. How do I change the column name and only selecting
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**


This may solve your problem:

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

After renaming the columns you are using the old list of columns defined thus that will solve the error for the column name.

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

should be removed with

print(df_renamed)

I have managed to change the column name to the correct format.

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($)']])

However I am still not able to add in a new index number, remove the original excel column no. reference and format the decimal place for Sqm and Value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM