Pandas作业练习


```python
import pandas as pd
import numpy as np
#读数据
lianjia_df = pd.read_csv(r'lianjia9.csv')
#print(lianjia_df)

# 1. 观察结构,调整列索引顺序
#如何调整呢?,就是查找所有数据,将排好序的标签填进去,达到各列换位的一种效果
print(lianjia_df[["Region","Garden","Layout","Floor","Year","Size","Elevator","Direction","Renovation","Price"]].head())
lianjia_xiugai = lianjia_df[["Region","Garden","Layout","Floor","Year","Size","Elevator","Direction","Renovation","Price"]]
#创建一个新的变量存储列变换之后的数据
print(lianjia_xiugai)




# 2. 增加一个列关于目前状况(state),是否卖出状态随机
#读随机状态,有的是0有的是1,并不能一味地把所有的都设为1或0
import random
states = [0,1]
#shape函数是读取矩阵的长度,shape[0]是读取第一维度的数量,这里是指行数,即读取数据总行数
column_count = lianjia_df.shape[0]
#设置新的一列,random.choice是从非空序列中随机选取一个数据并带回
#python这个是真的方便
lianjia_df['state'] = [random.choice(states) for x in range(column_count)]

# 3. 删除id这一列无用的数据
# 备注:因为文件中的id不在了,所以先添加一个再删除一下
column_count = lianjia_df.shape[0]
# 先添加
lianjia_df['Id'] = [x for x in range(1,column_count+1)]
# 再删除
lianjia_df.drop("Id",axis=1,inplace=True)
# print(lianjia_df.head())








#python中字符串还能这么玩,牛逼
# floors = "我是低楼层"
# if "低楼层" in floors:
#     print("niubi")
``




# 4. 查找楼层小于7的房子(这里提取低楼层)
#定义一个函数,当楼层小于7的时候返回true,大于7返回false
def extract_low_floor(floors):
    #设置一个列表,用来存放最终结果
    low_floors = []
    for floor in floors:
        if "低楼层" in floor:
            low_floors.append(True)
        else:
            low_floors.append(False)
    return low_floors
#这里只会显示true的数据
low_floor_lianjia_df = lianjia_df[extract_low_floor(lianjia_df['Floor'])]
#print(low_floor_lianjia_df.head())

low_floor_line_count = low_floor_lianjia_df.shape[0]
print(low_floor_line_count)
#原文件可以直接设置
# lianjia_df['Elevator'] = "无电梯"
# print(lianjia_df.head())
#这里会报警告,意思是你在这上面进行改动,原来最初的版本的数据也会受到影响,但是我要的就是这个效果
low_floor_lianjia_df['Elevator'] = '无电梯'
print(low_floor_lianjia_df)`

你可能感兴趣的:(数据分析练习,python)