我有一个pandas数据框,它有两个datetime64列和一个timedelta64列,它们是两列之间的差异.我正在尝试绘制timedelta列的直方图,以显示两个事件之间的时间差异.
但是,只需使用df['time_delta']
结果:
TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('float64')
尝试将timedelta列转换为:float--> df2 = df1['time_delta'].astype(float)
结果:
TypeError: cannot astype a timedelta from [timedelta64[ns]] to [float64]
如何创建pandas timedelta数据的直方图?
我有一个看起来像这样的数据框:
data = {'index': ['2014-06-22 10:46:00', '2014-06-24 19:52:00', '2014-06-25 17:02:00', '2014-06-25 17:55:00', '2014-07-02 11:36:00', '2014-07-06 12:40:00', '2014-07-05 12:46:00', '2014-07-27 15:12:00'],
'type': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'C'],
'sum_col': [1, 2, 3, 1, 1, 3, 2, 1]}
df = pd.DataFrame(data, columns=['index', 'type', 'sum_col'])
df['index'] = pd.to_datetime(df['index'])
df = df.set_index('index')
df['weekofyear'] = df.index.weekofyear
df['date'] = df.index.date
df['date'] = pd.to_datetime(df['date'])
type sum_col weekofyear date
index
2014-06-22 10:46:00 A 1 25 2014-06-22
2014-06-24 19:52:00 B 2 26 2014-06-24
2014-06-25 17:02:00 C 3 …
Run Code Online (Sandbox Code Playgroud) 我试图了解python如何将数据从FTP服务器提取到pandas然后将其移动到SQL服务器.我的代码至少可以说是非常简陋的,我正在寻找任何建议或帮助.我试图从FTP服务器首先加载数据工作正常....如果我然后删除此代码并将其更改为从ms sql服务器中选择它是好的所以连接字符串工作,但插入到SQL服务器似乎造成了问题.
import pyodbc
import pandas
from ftplib import FTP
from StringIO import StringIO
import csv
ftp = FTP ('ftp.xyz.com','user','pass' )
ftp.set_pasv(True)
r = StringIO()
ftp.retrbinary('filname.csv', r.write)
pandas.read_table (r.getvalue(), delimiter=',')
connStr = ('DRIVER={SQL Server Native Client 10.0};SERVER=localhost;DATABASE=TESTFEED;UID=sa;PWD=pass')
conn = pyodbc.connect(connStr)
cursor = conn.cursor()
cursor.execute("INSERT INTO dbo.tblImport(Startdt, Enddt, x,y,z,)" "VALUES (x,x,x,x,x,x,x,x,x,x.x,x)")
cursor.close()
conn.commit()
conn.close()
print"Script has successfully run!"
Run Code Online (Sandbox Code Playgroud)
当我删除ftp代码时,它运行完美,但我不明白如何进行下一次跳转以将其转换为Microsoft SQL服务器,或者即使可以在不先保存到文件中也是如此.
我想检查一个系列中的单词是否以系列的strings
一个单词结尾ending_strings
.
strings = Series(['om', 'foo_nom', 'nom', 'bar_foo', 'foo','blah'])
ending_strings = Series(['nom', 'foo'])
expected_results = Series([False, True, True, True, True, False])
Run Code Online (Sandbox Code Playgroud)
我已经提出了以下代码,但有没有更快或更多的熊猫风格的方式来做到这一点?
from pandas import Series
def ew(v):
return strings.str.endswith(v)
result = ending_strings.apply(ew).apply(sum).astype(bool)
result.equals(expected_results)
Run Code Online (Sandbox Code Playgroud) 编辑2
如果有人可以发布架构应该是什么,我会非常高兴!我只需要知道表名和列名!
我正在按照本教程进行操作:
http://www.caktusgroup.com/blog/2014/06/23/scheduling-tasks-celery/
我成功地安装了django-celery.
#settings.py
import djcelery
djcelery.setup_loader()
BROKER_URL = 'django://'
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'charts',
'social.apps.django_app.default',
'django.contrib.staticfiles',
'djcelery',
'kombu.transport.django',
)
Run Code Online (Sandbox Code Playgroud)
当我跑python manage.py syncdb
:
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table social_auth_usersocialauth
Creating table social_auth_nonce
Creating table social_auth_association
Creating table social_auth_code
Creating table celery_taskmeta
Creating table celery_tasksetmeta …
Run Code Online (Sandbox Code Playgroud) 使用ReactJS和Material UI,我有一个项目,我在其中更改了主题颜色.
const newTheme = getMuiTheme({
fontFamily: 'Roboto, sans-serif',
palette: {
primary1Color: cyan500,
primary2Color: cyan700,
primary3Color: grey400,
accent1Color: amberA400,
accent2Color: grey100,
accent3Color: grey500,
textColor: darkBlack,
alternateTextColor: white,
canvasColor: white,
borderColor: grey300,
disabledColor: fade(darkBlack, 0.3),
pickerHeaderColor: cyan500,
clockCircleColor: fade(darkBlack, 0.07),
shadowColor: fullBlack,
},
});
// App class
render() {
return(
<MuiThemeProvider muiTheme={newTheme}>
<Project />
</MuiThemeProvider>
)
}
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作.某些组件(如按钮)可以根据主要道具设置颜色.但是,我有一个使用需要主色的图标的组件.我可以导入颜色并直接设置它:
import React from 'react';
import ActionLock from 'material-ui/svg-icons/action/lock';
import {cyan500} from 'material-ui/styles/colors';
export default class LockIcon extends React.Component {
render() {
return(
<ActionLock …
Run Code Online (Sandbox Code Playgroud) 我有一个包含lat/lon坐标列表的数据框:
d = {'Provider ID': {0: '10001',
1: '10005',
2: '10006',
3: '10007',
4: '10008',
5: '10011',
6: '10012',
7: '10016',
8: '10018',
9: '10019'},
'latitude': {0: '31.215379379000467',
1: '34.22133455500045',
2: '34.795039606000444',
3: '31.292159523000464',
4: '31.69311635000048',
5: '33.595265517000485',
6: '34.44060759100046',
7: '33.254429322000476',
8: '33.50314015000049',
9: '34.74643089500046'},
'longitude': {0: ' -85.36146587999968',
1: ' -86.15937514799964',
2: ' -87.68507485299966',
3: ' -86.25539902199966',
4: ' -86.26549483099967',
5: ' -86.66531866799966',
6: ' -85.75726760699968',
7: ' -86.81407933399964',
8: ' -86.80242858299965',
9: ' -87.69893502799965'}}
df = pd.DataFrame(d) …
Run Code Online (Sandbox Code Playgroud) 当我运行下面的行时,数据框中的 NaN 数字不会被修改。使用与 完全相同的参数.to_csv()
,我得到了预期的结果。.to_html
需要不同的东西吗?
df.to_html('file.html', float_format='{0:.2f}'.format, na_rep="NA_REP")
当我在我拥有的数据框架上创建数据透视表时,传递aggfunc='mean'
按预期aggfunc='count'
工作,按预期工作,但aggfunc=['mean', 'count']
结果如下:AttributeError: 'str' object has no attribute '__name__
这种格式似乎先前有效:Pandas中的多个AggFun
如何创建具有多个功能的数据透视表?
df.to_dict()
创建一个嵌套字典,其中标头形成键{column:{index:value}}
.
有没有一种简单的方法来创建一个字典,其中索引形成键{index:column:value}}
?甚至{index:(column,value)}
?
我可以创建字典然后反转它,但我想知道这是否可以一步完成。
python ×9
pandas ×8
dataframe ×4
celery ×1
django ×1
material-ui ×1
matplotlib ×1
performance ×1
pyodbc ×1
reactjs ×1
sql ×1