目录
1.列 基础操作 :
import pandas as pd
import numpy as np
d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
# 筛选one列数据
print(df['one'])
# 新增列,并添加数据
df['three'] = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(df)
# 两列数据相加
df['four'] = df['one'] + df['three']
print(df)
# 删除one 列数据
del df['one']
print(df)
结果:
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
one two three four
a 1.0 1 10.0 11.0
b 2.0 2 20.0 22.0
c 3.0 3 30.0 33.0
d NaN 4 NaN NaN
two three four
a 1 10.0 11.0
b 2 20.0 22.0
c 3 30.0 33.0
d 4 NaN NaN
2.行 基础操作
import pandas as pd
d = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print("1---------------")
print(df)
print("2筛选索引是b 行数据---------------")
print(df.loc['b'])
print("3筛选索引是1 行数据---------------")
print(df.iloc[1])
print("4筛选索引2和3 行数据---------------")
print(df[2:4])
print("5追加数据---------------")
df = df.append(pd.DataFrame([[5, 6], [7, 8]], columns=['one', 'two']))
print(df)
print("6删除索引为0的行---------------")
df = df.drop(0)
print(df)
结果:
1---------------
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
2筛选索引是b 行数据---------------
one 2.0
two 2.0
Name: b, dtype: float64
3筛选索引是1 行数据---------------
one 2.0
two 2.0
Name: b, dtype: float64
4筛选索引2和3 行数据---------------
one two
c 3.0 3
d NaN 4
5追加数据---------------
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
0 5.0 6
1 7.0 8
6删除索引为0的行---------------
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
1 7.0 8
3.行的交换,顺序更换:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(25).reshape(5, -1))
print(df)
a, b = df.iloc[1].copy(), df.iloc[2].copy()
df.iloc[1], df.iloc[2] = b, a
print("-----------------------")
print(df)
结果:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
-----------------------
0 1 2 3 4
0 0 1 2 3 4
1 10 11 12 13 14
2 5 6 7 8 9
3 15 16 17 18 19
4 20 21 22 23 24
4.Series基本操作
import pandas as pd
import numpy as np
data = pd.Series(np.random.randint(0,4,5))
print("-----------")
print(data)
print("返回行轴标签列表-----------")
print(data.axes)
print("如果系列为空,则返回True-----------")
print(data.empty)
print("返回底层数据的维数,默认定义:1-----------")
print(data.ndim)
print("返回基础数据中的元素数-----------")
print(data.size)
print("将系列作为ndarray返回-----------")
print(data.values)
print("返回前n行-----------")
print(data.head(3))
print("返回最后n行-----------")
print(data.tail(2))
结果:
-----------
0 2
1 0
2 0
3 1
4 0
dtype: int32
返回行轴标签列表-----------
[RangeIndex(start=0, stop=5, step=1)]
如果系列为空,则返回True-----------
False
返回底层数据的维数,默认定义:1-----------
1
返回基础数据中的元素数-----------
5
将系列作为ndarray返回-----------
[2 0 0 1 0]
返回前n行-----------
0 2
1 0
2 0
dtype: int32
返回最后n行-----------
3 1
4 0
dtype: int32
5.DataFrame基本操作
import pandas as pd
# Create a Dictionary of series
d = {'Name': pd.Series(['Tom', 'James', 'Ricky', 'Vin', 'Steve', 'Minsu', 'Jack']),
'Age': pd.Series([25, 26, 25, 23, 30, 29, 23]),
'Rating': pd.Series([4.23, 3.24, 3.98, 2.56, 3.20, 4.6, 3.8])}
# Create a DataFrame
data = pd.DataFrame(d)
print(data)
print("获取列名")
print(data.columns)
print("转置行和列--------------")
print(data.T)
print("返回一个列,行轴标签和列轴标签作为唯一成员--------------")
print(data.axes)
print("返回此对象中的数据类型(dtypes)")
print(data.dtypes)
print("如果NDFrame完全为空【无项目】,则返回为True;如果任何轴的长度为0")
print(data.empty)
print("轴/数组维度大小")
print(data.ndim)
print("返回表示DataFrame的维度的元组")
print(data.shape)
print("NDFrame中元素数")
print(data.size)
print("NDFrame的Bunpy表示")
print(data.values)
print("返回开头前n行")
print(data.head(3))
print("返回最后n行")
print(data.tail(2))
结果:
D:\Python36\python.exe C:/Users/17653/Desktop/测试.py
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
5 Minsu 29 4.60
6 Jack 23 3.80
获取列名
Index(['Name', 'Age', 'Rating'], dtype='object')
转置行和列--------------
0 1 2 3 4 5 6
Name Tom James Ricky Vin Steve Minsu Jack
Age 25 26 25 23 30 29 23
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
返回一个列,行轴标签和列轴标签作为唯一成员--------------
[RangeIndex(start=0, stop=7, step=1), Index(['Name', 'Age', 'Rating'], dtype='object')]
返回此对象中的数据类型(dtypes)
Name object
Age int64
Rating float64
dtype: object
如果NDFrame完全为空【无项目】,则返回为True;如果任何轴的长度为0
False
轴/数组维度大小
2
返回表示DataFrame的维度的元组
(7, 3)
NDFrame中元素数
21
NDFrame的Bunpy表示
[['Tom' 25 4.23]
['James' 26 3.24]
['Ricky' 25 3.98]
['Vin' 23 2.56]
['Steve' 30 3.2]
['Minsu' 29 4.6]
['Jack' 23 3.8]]
返回开头前n行
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
返回最后n行
Name Age Rating
5 Minsu 29 4.6
6 Jack 23 3.8
Process finished with exit code 0