【发布时间】:2021-03-29 22:01:03
【问题描述】:
以下数据是根据一些用户输入提取的。
- 如何将索引更改为从第 1 个 iso 列 ID 开始。编号列是新的。
- 如何更改列名并仅选择
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