import pandas
as pd
frame.columns
frame.index
frame.T
frame.head(num)
frame.tail(num)
frame.info()
frame.rename(columns=
lambda x: x.replace(
'xx',
'yy'), inplace=
True)
frame.loc[i]
frame.loc[i][j]
frame[
'key']
frame[i:]
frame[
'key'][i]
frame.loc[i].tolist()
frame.pop(
'key')
DataFrame的连接方式
frame_x, frame_y分别为需要连接的两个dataframe
how是决定了用何种方法连接,常见的有left(左外连接)、right(右外连接)、inner(交集,默认)、outer(并集)
on是决定连接的key,在这里两个frame依靠’key’这列合并,多列合并需要传入列表,可省略
new_frame = pd.merge(frame_x, frame_y, how=
'left', on=
'key')
frame_x.join(frame_y, on=
'key', how=
'left')
new_frame = pd.concat(frame_x, frame_y, join=
'outer', axai=
1)
读取存储DataFrame为csv格式
frame_read = pd.read_csv(csv_file, sep=
',' ,encoding=xxx, engine=
'python')
frame_save.to_csv(csv_file)
转为dataframe
import numpy
as np
arr = np.arange(
5)
df = pd.DataFrame(arr, columns=[
'key'])
dict = {
'key1':
'value1',
'key2':
'value2',
}
df = pd.DataFrame(dict, columns=[
'key1',
'key2'])
下面部分的参考资料
数据结构
Series
一维数组对象
obj = Series([
1,
2,
3,
4])
print(obj)
表示形式为:索引在左,值在右
print(obj.values)
print(obj.index)
创建带有索引的Series
obj2 = Series([
1,
2,
3,
4], index=[
'a',
'b',
'c',
'd'])
print(obj2)
print(obj2.index)
检索
print(obj2[
'a'])
print(obj2[[
'a',
'b',
'c']])
过滤数据
obj2[obj2 >
0]
obj2 *
2
np.exp(obj2)
判断索引是否存在
print(
'b' in obj2)
print(
'e' in obj2)
字典传递
dict_ser = {
'a':
1,
'b':
2,
'c':
3,
'd':
4}
obj3 = Series(dict_ser)
dict_index = [
'e',
'b',
'c',
'd']
obj4 = Series(dict_ser, index=dict_index)
print(obj4)
数据丢失判断
print(pd.isnull(obj3))
print(pd.notnull(obj4))
Series合并
Series对象和它的索引都有一个name属性
obj5 = obj3 + obj4
obj4.name =
'value'
obj4.index.name =
'word'
obj4.index = [
'a1',
'b1',
'c1',
'd1']
DataFrame
类似一个电子表格的数据结构,可以看作为Series的字典(每一个Series共享一个索引)
data = {
'key1': [
'a',
'b',
'c',
'd'],
'key2': [
1,
2,
3,
4],
'key3': [
1.1,
2.2,
3.3 ,
4.4]}
df = DataFrame(data)
print(df)
df = DataFrame(data, columns=[
'value1',
'value2',
'value3'])
df = DataFrame(data, columns=[
'value1',
'value2',
'value3',
'value4'], index=[
'one',
'two',
'three',
'four'])
检索具体值
print(df[
'value1'])
print(df.value2)
检索行
print(df.ix[
'three'])
修改列
df[
'value4'] =
1
df[
'value4'] = np.arange(
5.)
val = Series(
'A',
'B',
'C', index=[
'two',
'one',
'three'])
df[
'value4'] = val
df[
'value5'] = df.value1 ==
'b'
print(df.columns)
del df[
'value5']
print(df.columns)
传递字典
data_dict = {
'key1': {
'value1':
1,
'value2':
2},
'key2': {
'value1':
3,
'value2':
4,
'value3':
5}}
df = DataFrame(data_dict)
print(df)
print(df.T)
df = DataFrame(data_dict, index=[
'a',
'b',
'c'])
print(df)
DataFrame的部分函数
df.index.name =
'name1'
df.columns.name =
'name2'
df.values
索引对象
索引对象是不可变的 重新索引需要使用reindex
obj = Series([
1,
2,
3,
4], index=[
'b',
'c',
'a',
'd'])
obj2 = obj.reindex([
'a',
'b',
'c',
'd',
'e'])
obj2 = obj.reindex([
'a',
'b',
'c',
'd',
'e'], fill_value=
0)
obj3 = Series([
'a',
'b',
'c'], index=[
0,
2,
4])
obj3.reindex(range(
6), method=
'ffill')
参数描述
ffill/pad前向(或进位)填充bfill/backfill后向(或进位)填充
columns = [
'key1',
'key2',
'key3']
df.reindex(columns=columns)
df.reindex(index=[...], columns=[...])
df.ix[[
'a',
'b',
'c',
'd'], name2]
删除条目
obj = Series(np.arange(
4.), index=[
'a',
'b',
'c',
'd'])
new_obj = obj.drop(
'c')
df = DataFrame(np.arange(
16).reshape(
4,
4),
index=[
'a',
'b',
'c',
'd'],
columns=[
'one',
'two',
'three',
'four'])
df.drop([
'a',
'c'])
df.drop([
'two',
'four'], axis=
1)
索引、挑选、过滤
obj = Series(np.arange(
4.), index=[
'a',
'b',
'c',
'd'])
print(obj[
'b'])
print(obj[
1])
print(obj[
2:
4])
print(obj[[
'b',
'a',
'd']])
print(obj[[
1,
3]])
print(obj[obj <
2])
print(obj[
'b':
'c'])
obj[
'b':
'c'] =
5
df = DataFrame(np.arange(
16).shape(
4,
4),
index=[
'a',
'b',
'c',
'd'],
columns=[
'one',
'two',
'three',
'four'])
print(df[
'two'])
print(df[[
'three',
'one']])
print(df[:
2])
print(df[df[
'three'] >
5])
print(df <
5)
df[df<
5] =
0
print(df.ix[
'b', [
'two',
'three']])
print(df.ix[[
'b',
'c'], [
3,
0,
1]])
print(df.ix[
2])
print(df.ix[:
'c',
'three'])
print(df.ix[df.three >
5, :
3])
算术方法
df1 + df2
对应索引和列中的值进行相加