1.1数据分析之------pandas

import  pandas  as pd
a=[1,4,7,9]
s=pd.Series(a)
s
0    1
1    4
2    7
3    9
dtype: int64
s.dtypes
dtype('int64')
s.values
array([1, 4, 7, 9], dtype=int64)
#修改索引
a=[1,4,7,9]
index=['a','b','c','d']
s=pd.Series(a,index)
s
a    1
b    4
c    7
d    9
dtype: int64
s.index
Index(['a', 'b', 'c', 'd'], dtype='object')
s
a    1
b    4
c    7
d    9
dtype: int64

a=[1,4,7,9]
index=list('rtyu')
s=pd.Series(a,index)
s
r    1
t    4
y    7
u    9
dtype: int64
s[s>5]
y    7
u    9
dtype: int64
dic = {
   'beijing':35000,'shanghai':71000,'guangzhou':16000,'shenzhen':5000}
s1=pd.Series(dic)
s1
beijing      35000
shanghai     71000
guangzhou    16000
shenzhen      5000
dtype: int64
s1['beijing']
35000
s1.keys()
Index(['beijing', 'shanghai', 'guangzhou', 'shenzhen'], dtype='object')
list(s1.items())
[('beijing', 35000),
 ('shanghai', 71000),
 ('guangzhou', 16000),
 ('shenzhen', 5000)]
s1.notnull()
beijing      True
shanghai     True
guangzhou    True
shenzhen     True
dtype: bool
pd.notnull(s1)
beijing      True
shanghai     True
guangzhou    True
shenzhen     True
dtype: bool
s1.name='xxxxxxx'
s1
beijing      35000
shanghai     71000
guangzhou    16000
shenzhen      5000
Name: xxxxxxx, dtype: int64
s1.index.name='qqqqqqqqqqq'
s1
qqqqqqqqqqq
beijing      35000
shanghai     71000
guangzhou    16000
shenzhen      5000
Name: xxxxxxx, dtype: int64
data = {
   'city':['beijing','beijing','beijing','shanghai','shanghai','shanghai'],
        'year':[2000,2001,2002,2001,2002,2003],
        'pop':[1.5, 1.7,3.6,2.4,2.9,3.2
    ]}
data
{'city': ['beijing', 'beijing', 'beijing', 'shanghai', 'shanghai', 'shanghai'],
 'year': [2000, 2001, 2002, 2001, 2002, 2003],
 'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
df=pd.DataFrame(data)
df
city year pop
0 beijing 2000 1.5
1 beijing 2001 1.7
2 beijing 2002 3.6
3 shanghai 2001 2.4
4 shanghai 2002 2.9
5 shanghai 2003 3.2
df.index
RangeIndex(start=0, stop=6, step=1)
df.columns
Index(['city', 'year', 'pop'], dtype='object')
df.dtypes
city     object
year      int64
pop     float64
dtype: object
df.values
array([['beijing', 2000, 1.5],
       ['beijing', 2001, 1.7],
       ['beijing', 2002, 3.6],
       ['shanghai', 2001, 2.4],
       ['shanghai', 2002, 2.9],
       ['shanghai', 2003, 3.2]], dtype=object)
df.head(1)
df.tail(2)
city year pop
4 shanghai 2002 2.9
5 shanghai 2003 3.2
df.columns
Index(['city', 'year', 'pop'], dtype='object')
df1=pd.DataFrame(data,columns=['year', 'city', 'pop'])

df1


year city pop
0 2000 beijing 1.5
1 2001 beijing 1.7
2 2002 beijing 3.6
3 2001 shanghai 2.4
4 2002 shanghai 2.9
5 2003 shanghai 3.2
df2 = pd.DataFrame(data,index=['a','b'
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,Python 可视化是一种用于创建图形化表示的技术,可以帮助我们更直观地理解数据。下面是一个关于丙型肝炎患者检测的示例。 首先,我们需要导入相关的数据分析和可视化库。这里我们使用 pandas 和 matplotlib。 ```python import pandas as pd import matplotlib.pyplot as plt ``` 接下来,我们读取数据集,数据集中包含了一些关于患者的信息,例如性别、年龄、是否有糖尿病等。 ```python data = pd.read_csv('hepatitis.csv') ``` 我们可以使用 data.head() 来查看前几行数据: ```python data.head() ``` 输出结果如下: ``` age sex steroid antivirals fatigue malaise anorexia liver_big liver_firm spleen_palpable spiders ascites varices bilirubin alk_phosphate sgot albumin protime histology class 0 30 2 1 2 2 2 2 1 2 2 2 2 2 1.00 85 18.0 4.0 1 2 1 50 1 1 2 1 2 2 1 2 2 2 2 2 0.90 135 42.0 3.5 1 2 2 78 1 2 2 1 2 2 2 2 2 2 2 2 0.70 96 32.0 4.0 1 2 3 31 1 1 1 2 2 2 2 2 2 2 2 2 0.70 46 52.0 4.0 1 2 4 34 1 2 2 2 2 2 2 2 2 2 2 2 1.00 105 200.0 4.0 1 2 ``` 其中,class 列是我们要预测的结果,1 表示患者是丙型肝炎患者,2 表示患者不是丙型肝炎患者。 接下来,我们可以使用柱状图来查看不同性别的患者数量: ```python sex_counts = data['sex'].value_counts() plt.bar(['Male', 'Female'], sex_counts) plt.title('Sex Distribution of Hepatitis Patients') plt.xlabel('Sex') plt.ylabel('Count') plt.show() ``` 输出结果如下: ![sex_distribution.png](https://img-blog.csdnimg.cn/c7f7c4d5adde4f2a9f3c5b9b4a8ed1b4.png) 我们还可以使用饼图来查看患者中是否有糖尿病: ```python diabetes_counts = data['diabetes'].value_counts() plt.pie(diabetes_counts, labels=['No Diabetes', 'Diabetes'], autopct='%1.1f%%') plt.title('Diabetes Distribution of Hepatitis Patients') plt.show() ``` 输出结果如下: ![diabetes_distribution.png](https://img-blog.csdnimg.cn/ebc2c3c725b14e9fa6e7a4a6a5f3a64f.png) 此外,我们还可以使用直方图来查看患者的年龄分布情况: ```python plt.hist(data['age'], bins=range(0, 100, 10)) plt.title('Age Distribution of Hepatitis Patients') plt.xlabel('Age') plt.ylabel('Count') plt.show() ``` 输出结果如下: ![age_distribution.png](https://img-blog.csdnimg.cn/1f8b9f5ec59c45c1bf0e5c25d4cbbd4b.png) 这些可视化图表可以帮助我们更好地理解数据,并且可以帮助我们做出更准确的预测和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值