2017-02-04 101 views
0

我有數據形式:如何搜索熊貓數據框以填充另一個數據框?

President    Years Executive Orders 
George Washington 1789-1797  8 
John Adams   1797-1801  1 
Thomas Jefferson 1801-1809  4 
       ... 

了歲月的字符串格式,我想創建一個新的數據幀中,每年進行重新取樣像下面這樣我就可以創建通過行政命令的陰謀這些年來(我想插,因爲數據不例如1801-1809之間給出數據):

Year Executive Orders 
1789   8 
1790   0 
1791   0 
... 

基本上我想要做像在第一DF在第二DF日期查找並看看有多少訂單。有任何想法嗎?

由於

回答

0
import pandas as pd 
import numpy as np 
from io import StringIO 

data = '''\ 
President    Years Executive Orders 
George Washington 1789-1797  8 
John Adams   1797-1801  1 
Thomas Jefferson 1801-1809  4 
''' 
df = pd.read_csv(StringIO(data), sep=r'\s+') 

df[['From', 'To']] = df['Executive'].str.split('-', expand=True) 
df['From'] = pd.to_datetime(df['From']) 
df['To'] = pd.to_datetime(df['To']) 

df_orders = df[['Orders', 'From']].set_index('From') 

這導致以下數據幀

  Orders 
From    
1789-01-01  8 
1797-01-01  1 
1801-01-01  4 

由於索引列是列resample可以用作需要重新取樣數據的日期。有關如何重新採樣數據,請參見docs

df_orders_resampled = df_orders.resample('AS').sum().fillna(0) 

      Orders 
From    
1789-01-01  8.0 
1790-01-01  0.0 
1791-01-01  0.0 
1792-01-01  0.0 
1793-01-01  0.0 
1794-01-01  0.0 
1795-01-01  0.0 
1796-01-01  0.0 
1797-01-01  1.0 
1798-01-01  0.0 
1799-01-01  0.0 
1800-01-01  0.0 
1801-01-01  4.0 

df_orders_resampled.plot() 

plot

相關問題