pandas 和 Series
代码
import pandas # 导入数据统计模块
data = ['A','B','C'] # 创建数据数组
index = ['a','b','c'] # 创建索引数组
series = pandas.Series(data,index=index) # 创建指定索引的 Series 对象
print(series)
print('索引数组为:',series.index)
print('元素数组为:',series.values)
输出
索引数组为: Index(['a', 'b', 'c'], dtype='object')
元素数组为: ['A' 'B' 'C']
代码
import pandas # 导入数据统计模块
data = ['A','B','C'] # 创建数据数组
index = ['a','b','c'] # 创建索引数组
series = pandas.Series(data,index=index) # 创建指定索引的 Series 对象
# 打印下标为 0,1,2 对应的 Series 对象
print('获取多个下标对应的 Series 对象:')
print(series[0:3])
输出
a A
b B
c C
代码
import pandas # 导入数据统计模块
data = ['A','B','C'] # 创建数据数组
index = ['a','b','c'] # 创建索引数组
series = pandas.Series(data,index=index) # 创建指定索引的 Series 对象
# 打印指定索引的 Series 对象
print('获取多个下标对应的 Series 对象:')
print(series[['a','c']])
输出
a A
c C
代码
import pandas # 导入数据统计模块
data = ['A','B','C'] # 创建数据数组
index = ['a','b','c'] # 创建索引数组
series = pandas.Series(data,index=index) # 创建指定索引的 Series 对象
# 修改下标为0的元素值
series[0] = 'D'
print('修改下标为0的元素值\n', series) # 打印修改元素值以后的 Series 对象
series['b'] = 'R' # 修改索引为 b 的元素值
print('修改索引为 b 的元素值:\n',series) # 打印修改元素值以后的 Series 对象
输出
修改下标为0的元素值
a D
b B
c C
dtype: object
修改索引为 b 的元素值:
a D
b R
c C
dtype: object
创建 DataFrame
代码
import pandas as pd # 导入数据统计模块
data = {
'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9]
}
data_frame = pd.DataFrame(data) # 创建 DataFrame 对象
print(data_frame)
输出
A B C
0 1 4 7
1 2 5 8
2 3 6 9
代码
from operator import index
import pandas as pd # 导入数据统计模块
data = {
'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9]
}
# 自定义索引
index = ['a','b','c']
data_frame = pd.DataFrame(data,index=index) # 创建 DataFrame 对象
print(data_frame)
输出
A B C
a 1 4 7
b 2 5 8
c 3 6 9
代码
from operator import index
import pandas as pd # 导入数据统计模块
data = {
'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9]
}
# 自定义索引
index = ['a','b','c']
data_frame = pd.DataFrame(data,columns=['B','C']) # 指定列创建 DataFrame
print(data_frame)
输出
B C
0 4 7
1 5 8
2 6 9
pandas 读取文件
代码
import pandas as pd
# r 可以转义 \
data = pd.read_excel(r'C:\Users\Administrator\Desktop\用户数据.csv')
print(data)
代码
from math import fabs
import pandas
import openpyxl
# 读取文件,并将文件存储到一个对象中(data)
data=pandas.read_excel(r'C:\Users\Administrator\Desktop\用户数据.xlsx')
print(data)
# 将文件中指定的列取出,存储到一个 excel 文件中
data.to_excel(r'C:\Users\Administrator\Desktop\新的用户数据.xlsx',columns=['编号','性别'],index=false)
# 读取文件
new_tongsang=pandas.read_excel(r'C:\Users\Administrator\Desktop\新的用户数据.xlsx')
print(new_tongsang)
作业
作业一
创建如下内容的DataFrame : 列名为[‘性别’,‘年龄’,‘专业’],索引为你和你的好朋友姓名,即创建一个你和你好朋友,性别年龄和所学专业的数据 DataFrame
作业一代码
from operator import index
from textwrap import indent
import pandas as pd
data = {
'sex':['男','女','男'],
'age':['17','18','19'],
'course':['工商管理','会计','园林'],
}
index = ['张三','李四','王五']
data_frame = pd.DataFrame(data,index=index)
# 取列
print(data_frame['sex'])
# 取行
print(data_frame.iloc[1,:])
>>> # 取列
>>>
>>> print(data_frame['sex'])
张三 男
李四 女
王五 男
Name: sex, dtype: object
>>> # 取行
>>>
>>> print(data_frame.iloc[1,:])
sex 女
age 18
course 会计
Name: 李四, dtype: object
作业二
读取作业数据,获得“活跃用户数据”这一sheet 的数据,获取性别,年龄,地区三列数据,并将其输出
import pandas as pd
# r 可以转义 \
data = pd.read_excel(r'C:\Users\Administrator\Desktop\作业数据.xlsx',sheet_name="活跃用户数据")
print(data)
data.to_csv(r'C:\Users\Administrator\Desktop\新的作业数据.csv',columns=data[[['性别','年龄阶层','地区']]])
new_data=pd.read_csv(r'C:\Users\Administrator\Desktop\新的作业数据.csv')
print(new_data)
输出
Unnamed: 0 性别 年龄阶层 地区
0 0 1 6 1
1 1 1 7 4
2 2 1 5 3
3 3 1 5 3
4 4 1 6 2
... ... .. ... ..
5950 5950 1 3 2
5951 5951 1 3 5
5952 5952 0 4 2
5953 5953 1 7 2
5954 5954 1 3 2
本文介绍了如何使用pandas创建Series对象,指定索引,并操作数据,包括索引访问、修改元素值,以及DataFrame的创建、定制索引和列选择。涵盖了数据结构的基础操作和数据组织。

979

被折叠的 条评论
为什么被折叠?



