给定两个dataframes df_1
和df_2
如何加入他们的行列,使得datetime列df_1
是介于两者之间start
,并end
在数据帧df_2
:
print df_1
timestamp A B
0 2016-05-14 10:54:33 0.020228 0.026572
1 2016-05-14 10:54:34 0.057780 0.175499
2 2016-05-14 10:54:35 0.098808 0.620986
3 2016-05-14 10:54:36 0.158789 1.014819
4 2016-05-14 10:54:39 0.038129 2.384590
print df_2
start end event
0 2016-05-14 10:54:31 2016-05-14 10:54:33 E1
1 2016-05-14 10:54:34 2016-05-14 10:54:37 E2
2 2016-05-14 10:54:38 2016-05-14 10:54:42 E3
Run Code Online (Sandbox Code Playgroud)
获取相应的event
位置和df1.timestamp
之间df_2.start
df2.end
timestamp A B event
0 2016-05-14 10:54:33 …
Run Code Online (Sandbox Code Playgroud) list
在应用str.findall()
到pandas数据帧的列之后,我想出了方括号中的值(更像是a ).如何拆下方括号?
print df
id value
1 [63]
2 [65]
3 [64]
4 [53]
5 [13]
6 [34]
Run Code Online (Sandbox Code Playgroud) 我有一个bash脚本,它创建一个我想在Airflow中运行的文件(如果它不存在),但是当我尝试它失败时.我该怎么做呢?
#!/bin/bash
#create_file.sh
file=filename.txt
if [ ! -e "$file" ] ; then
touch "$file"
fi
if [ ! -w "$file" ] ; then
echo cannot write to $file
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
这就是我在Airflow中调用它的方式:
create_command = """
./scripts/create_file.sh
"""
t1 = BashOperator(
task_id= 'create_file',
bash_command=create_command,
dag=dag
)
lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 83, in execute
raise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command failed
Run Code Online (Sandbox Code Playgroud) 如何将日期时间舍入到前一个小时?例如:
print datetime.now().replace(microsecond=0)
>> 2017-01-11 13:26:12.0
Run Code Online (Sandbox Code Playgroud)
向下舍入到前一个小时: 2017-01-11 12:00:00.0
这是我的问题的后续跟进.而不是数据透视表,是否可以将表格展平为如下所示:
data = {'year': ['2016', '2016', '2015', '2014', '2013'],
'country':['uk', 'usa', 'fr','fr','uk'],
'sales': [10, 21, 20, 10,12],
'rep': ['john', 'john', 'claire', 'kyle','kyle']
}
pd.DataFrame(data).pivot_table(index='country', columns='year', values=['rep','sales'])
rep sales
year 2013 2014 2015 2016 2013 2014 2015 2016
country
fr None kyle claire None None 10 20 None
uk kyle None None john 12 None None 10
usa None None None john None None None 21
Run Code Online (Sandbox Code Playgroud)
扁平表:
rep_2013 rep_2014 rep_2015 rep_2016 sales_2013 sales_2014 sales_2015 sales_2016
country
fr None kyle …
Run Code Online (Sandbox Code Playgroud) 我有一个巨大的熊猫数据帧我转换为html表,即dataframe.to_html()
,它约1000行.任何使用分页的简单方法,这样我就不必滚动整行1000行.比如说,查看前50行然后单击下一步以查看后续的50行?
我想从与列下面的数据帧进行透视表sales
,rep
.数据透视表显示sales
但没有rep
.当我只尝试时rep
,我得到了错误DataError: No numeric types to aggregate
.如何解决这个问题,我看到数字字段sales
和字段(字符串)rep
data = {'year': ['2016', '2016', '2015', '2014', '2013'],
'country':['uk', 'usa', 'fr','fr','uk'],
'sales': [10, 21, 20, 10,12],
'rep': ['john', 'john', 'claire', 'kyle','kyle']
}
print pd.DataFrame(data).pivot_table(index='country', columns='year', values=['rep','sales'])
sales
year 2013 2014 2015 2016
country
fr NaN 10 20 NaN
uk 12 NaN NaN 10
usa NaN NaN NaN 21
print pd.DataFrame(data).pivot_table(index='country', columns='year', values=['rep'])
DataError: No numeric types to aggregate
Run Code Online (Sandbox Code Playgroud) 如何在散景中使用D3调色板?我尝试以这种方式导入但是我得到了一个未解决的引用错误消息
from bokeh.palettes import Category20
Run Code Online (Sandbox Code Playgroud)
散景版:
print bokeh.__version__
0.11.1
Run Code Online (Sandbox Code Playgroud) 我想按特定列对包含许多列的数据帧进行排序,但首先我需要将类型更改object
为int
.如何在保留原始列位置的同时更改此特定列的数据类型?
鉴于有两个日期时间列的数据帧A
,并B
和数字列C
,如何组由month
两个A
与B
和sum(C)
即
In [1]: df
Out[1]:
A B C
0 2013-01-01 2013-01-01 0.282863
1 2013-01-02 2013-01-01 0.173215
2 2013-02-03 2013-02-04 2.104569
3 2013-02-09 2013-04-15 0.706771
4 2013-03-05 2013-08-01 0.567020
5 2013-03-06 2013-04-01 0.113648
Run Code Online (Sandbox Code Playgroud)