python-pandas模块

import pandas as pd
import numpy as np
import os
df = pd.DataFrame(columns=['A', 'B', 'C'], index=[0, 1, 2])
print(df)
'''
创建空的DataFrame
     A    B    C
0  NaN  NaN  NaN
1  NaN  NaN  NaN
2  NaN  NaN  NaN
'''
df = pd.DataFrame(data=[['Apple', 5],
                        ['Banana', 10],
                        ['cherry', 8],
                        ['Dates', 3],
                        ['Eggfruit', 7]],
                  columns=['Fruits', 'Quantity'],
                  index=[1, 2, 3, 4, 5])
print(df)
'''
手动创建DataFrame
Fruits  Quantity
1     Apple         5
2    Banana        10
3    cherry         8
4     Dates         3
5  Eggfruit         7
'''
fruits_list = ['Apple', 'Banana', 'Cherry', 'Dates', 'Eggfruits']
df = pd.DataFrame(fruits_list,
                  columns=['Fruits'],
                  index=['a', 'b', 'c', 'd', 'e'])
print(df)
'''
使用列表创建DataFrame
   Fruits
a      Apple
b     Banana
c     Cherry
d      Dates
e  Eggfruits
'''
fruits_dict = {'Fruits': ['Apple', 'Banana', 'Cherry', 'Dates', 'Eggfruit'],
               'Quantity': [5, 10, 8, 3, 7],
               'Color': ['Red', 'Yellow', 'Red', 'Brown', 'Yellow']}
df = pd.DataFrame(fruits_dict)
print(df)
'''
使用字典创建DataFrame
 Fruits  Quantity   Color
0     Apple         5     Red
1    Banana        10  Yellow
2    Cherry         8     Red
3     Dates         3   Brown
4  Eggfruit         7  Yellow
'''
df = pd.DataFrame([[1, 2], [2, 4]])
print('index:', df.index)   # 行标签
print('index name:', df.index.name)  # 行标签的名字
print('colums dtype:', df.columns.dtype)  # 列标签的数据类型
df = pd.DataFrame(data=np.arange(12).reshape(-1, 3), index=list('ABCD'), columns=['aa', 'ab', 'ac'])
print(df)
df.rename(columns={'aa': 'Aa'})  # 修改部分列名
print(df)
import pandas as pd                                                                                                    
import numpy as np                                                                                                     
import matplotlib.pyplot as plt                                                                                        
# 可以通过传递一个list对象来创建一个Series,pandas会默认创建整型索引                                                                            
s = pd.Series([1, 3, 5, np.nan, 6, 8])                                                                                 
print(s)                                                                                                               
'''                                                                                                                    
  0    1.0                                                                                                             
  1    3.0                                                                                                             
  2    5.0                                                                                                             
  3    NaN                                                                                                             
  4    6.0                                                                                                             
  5    8.0                                                                                                             
  dtype: float64                                                                                                       
'''                                                                                                                    
# 通过传递一个numpy array,时间索引以及列标签来创建一个DataFrame                                                                            
dates = pd.date_range('20130101', periods=6)                                                                           
print(dates)                                                                                                           
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))  # index:列标签 columns为行标签                   
print(df)                                                                                                              
name = ['计算机应用技术', '计算机网络技术', '软件技术', '云计算应用技术', '数字媒体技术', '大数据应用技术']                                                  
df = pd.DataFrame(np.random.randn(6, 6), index=name, columns=dates)                                                    
print(df)                                                                                                              
# 通过传递一个能够被转换成类似序列结构的字典对象来创建一个DataFrame                                                                                
df2 = pd.DataFrame({'A': 1,                                                                                            
                    'B': pd.Timestamp('20130102'),                                                                     
                    'C': pd.Series(1, index=list(range(4)), dtype='float32'),                                          
                    'D': np.array([3] * 4, dtype='int32'),                                                             
                    'E': pd.Categorical(["test", "train","test", "train"]),                                            
                    'F': 'foo'                                                                                         
                   })                                                                                                  
print(df2)                                                                                                             


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值