我想计算数据帧中某些单词的出现次数.我知道使用"str.contains"
a = df2[df2['col1'].str.contains("sample")].groupby('col2').size()
n = a.apply(lambda x: 1).sum()
Run Code Online (Sandbox Code Playgroud)
目前我正在使用上面的代码.有没有一种方法可以匹配正则表达式并获取出现次数?在我的情况下,我有一个大型数据帧,我想匹配大约100个字符串.
我正在尝试合并两个DataFrames求和列值.
DF1
id name weight
1 A 0
2 B 10
3 C 10
Run Code Online (Sandbox Code Playgroud)
DF2
id name weight
2 B 15
3 C 10
Run Code Online (Sandbox Code Playgroud)
我需要在合并期间对公共列中的类似值求和权重值.
merge = pd.merge(df1,df2, how='inner')
Run Code Online (Sandbox Code Playgroud)
所以输出将如下所示.
id name weight
2 B 25
3 C 20
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用'.''扩展名的'upload'目录中的最新文件来处理Python.我使用UbuntuWeb服务器,文件上传由html脚本完成.上载的文件由Python脚本处理,结果将写入MySQL数据库.我将这个答案用于我的代码.
import glob
newest = max(glob.iglob('upload/*.log'), key=os.path.getctime)
print newest
f = open(newest,'r')
Run Code Online (Sandbox Code Playgroud)
但这并没有获得目录中的最新文件,而是获得最旧的文件.为什么?
我正在尝试DataFrame用NaN值连接Pandas 列.
In [96]:df = pd.DataFrame({'col1' : ["1","1","2","2","3","3"],
'col2' : ["p1","p2","p1",np.nan,"p2",np.nan], 'col3' : ["A","B","C","D","E","F"]})
In [97]: df
Out[97]:
col1 col2 col3
0 1 p1 A
1 1 p2 B
2 2 p1 C
3 2 NaN D
4 3 p2 E
5 3 NaN F
In [98]: df['concatenated'] = df['col2'] +','+ df['col3']
In [99]: df
Out[99]:
col1 col2 col3 concatenated
0 1 p1 A p1,A
1 1 p2 B p2,B
2 2 p1 C p1,C
3 2 NaN …Run Code Online (Sandbox Code Playgroud) 我正在分析Apache日志文件,我已将其导入到pandas数据帧中.
'65 .55.52.118 - - [30/May/2013:06:58:52 -0600]"GET /detailedAddVen.php?refId=7954&uId=2802 HTTP/1.1"200 4514" - ""Mozilla/5.0(兼容; bingbot) /2.0; + http://www.bing.com/bingbot.htm)"'
我的数据帧:

我想根据IP,代理和时差将其分组到会话中(如果持续时间大于30分钟则应该是新会话).
通过IP和Agent很容易对数据帧进行分组,但是如何检查这个时间差?希望问题很清楚.
sessions = df.groupby(['IP', 'Agent']).size()
Run Code Online (Sandbox Code Playgroud)
更新:df.index如下:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-05-30 06:00:41, ..., 2013-05-30 22:29:14]
Length: 31975, Freq: None, Timezone: None
Run Code Online (Sandbox Code Playgroud) 我想计算数据帧不包含某些字符串的行.例如:
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w']]).T
df.columns = ['col1','col2','col3']
col1 col2 col3
0 1.1 A x/y/z
1 1.1 A x/y
2 1.1 A x/y/z/n
3 2.6 B x/u
4 2.5 B x
5 3.4 B x/u/v
6 2.6 B x/y/z
7 2.6 A x
8 3.4 B x/u/v/b
9 3.4 C -
10 2.6 B x/y
11 1.1 D x/y/z
12 1.1 D x
13 3.3 D x/u/v/w
Run Code Online (Sandbox Code Playgroud)
在上面的数据框中,我想计算不包含'u'或'z'的行.我知道如何使用str.contains来获取具有特定字符串的行.
df.col3.str.contains('u|z')
Run Code Online (Sandbox Code Playgroud)
如何计算"不"部分?
I want to send a pandas dataframe data as an HTML e-mail. Based on this post I could create an html with the dataframe. Code
import pandas as pd
import numpy as np
HEADER = '''
<html>
<head>
</head>
<body>
'''
FOOTER = '''
</body>
</html>
'''
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD')]).T
with open('test.html', 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
Run Code Online (Sandbox Code Playgroud)
Now I want to send this as a html e-mail. I tried this. Can not …
我正在分析网络服务器日志文件并具有以下格式的日期时间。
02/Apr/2013:23:55:00 +0530
Run Code Online (Sandbox Code Playgroud)
我正在将其转换为 pandas 日期时间格式。
df['Time'] = pd.to_datetime(df['Time'])
Run Code Online (Sandbox Code Playgroud)
但它仍然是对象格式。
print df.dtypes
Run Code Online (Sandbox Code Playgroud)
时间对象
为什么它没有改变为datetime64[ns]?
numpy 版本
In [2]: np.__version__
Out[2]: '1.8.0'
Run Code Online (Sandbox Code Playgroud) 我有一个像下面这样的pandas DataFrame.
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8], ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w'],['1','3','3','2','4','2','5','3','6','3','5','1','1','1'],['200','400','404','200','200','404','200','404','500','200','500','200','200','400']]).T
df.columns = ['col1','col2','col3','col4','ID','col5']
Run Code Online (Sandbox Code Playgroud)
我希望通过"ID"对此进行分组,并获得每组的第2行.后来我还需要获得第3名和第4名.只是解释一下如何只获得每组的第二行.
我试过以下给出第一和第二.
df.groupby('ID').head(2)
Run Code Online (Sandbox Code Playgroud)
相反,我只需要获得第二行.由于ID 4和6没有第二行需要忽略它们.
col1 col2 col3 col4 ID col5
ID
1 0 1.1 A 1.1 x/y/z 1 200
11 1.1 D 4.7 x/y/z 1 200
2 3 2.6 B 2.6 x/u 2 200
5 3.4 B 3.8 x/u/v 2 404
3 1 1.1 A 1.7 x/y 3 400
2 1.1 A 2.5 x/y/z/n 3 404
4 4 …Run Code Online (Sandbox Code Playgroud) 在Pandas中DataFrame如何使用整数映射一列中的字符串.我有大约500个字符串,DataFrame需要用"1"开头的整数替换它们.
样品DataFrame.
Request count
547 GET /online/WebResource.axd 37506
424 GET /online/2/2/22001.aspx 13315
699 POST /online/2/6/1/261001.aspx 13236
546 GET /online/ScriptResource.axd 12255
492 GET /online/2/6/Home.aspx 10462
660 POST /online/2/2/22001.aspx 9803
Run Code Online (Sandbox Code Playgroud)
我已将所有请求列入清单.
requestlist = df.Request.unique()
Run Code Online (Sandbox Code Playgroud)
不知道如何使用1-500映射这些请求.类似的问题.python pandas用数字替换数据帧中的字符串