0
点赞
收藏
分享

微信扫一扫

Python中pd.to_datetime、groupby、range(len())

color_小浣熊 2022-02-17 阅读 41

目录

1 pd.to_datetime

2 groupby

2.1 groupby函数功能

2.3 举例

3 range(len())


今日份笔记:

1 pd.to_datetime

print(pd.to_datetime('2018-07'))

输出:2018-07-01 00:00:00
该方法将时间戳或日期统一输出为格式:年月日时分秒。 

2 groupby

#=========1创建数据=========
import pandas as pd
import numpy as np
df = pd.DataFrame({'key1':['a', 'a', 'b', 'b', 'a'],
     'key2':['one', 'two', 'one', 'two', 'one'],
     'data1':np.random.randn(5),
     'data2':np.random.randn(5)})
print(df)

#=======2进行拆分==========
grouped=df['data1'].groupby(df['key1'])
print(grouped)

#=======3进行聚合计算=====
print(grouped.mean())
Testing started at 23:00 ...

  key1 key2     data1     data2
0    a  one -0.919658  0.450728
1    a  two  0.031374  1.066755
2    b  one -2.269761 -0.099858
3    b  two -0.690892  0.596098
4    a  one -1.251843 -1.643419
<pandas.core.groupby.generic.SeriesGroupBy object at 0x000000000B47A430>
key1
a   -0.713376
b   -1.480327
Name: data1, dtype: float64

3 range(len())

a=("123","456","789")
for i in range(len(a)):
    print(i,a)
 
运行结果:
0 ('123', '456', '789')
1 ('123', '456', '789')
2 ('123', '456', '789')
for i in range(0, len(arr))

#这里发生的是您获得的增量值基于数组的大小,这样您就可以将该值用作列表的索引。

 

 

举报

相关推荐

0 条评论