+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-03-06 19:51     阅读(521)     评论(0)     点赞(4)     收藏(4)


0

1

2

3

4

5

6

7

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)

0

1

2

3

4



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/879601/9ff916efd02a19d164c4/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)