Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何生成一个新的数据帧,将一些行压缩到一个新列中?_Python_Pandas_Dataframe_Time Series - Fatal编程技术网

Python 如何生成一个新的数据帧,将一些行压缩到一个新列中?

Python 如何生成一个新的数据帧,将一些行压缩到一个新列中?,python,pandas,dataframe,time-series,Python,Pandas,Dataframe,Time Series,我对pandas DataFrame相当陌生,但我一直在看教程和阅读有关它的文档,我无法完全想出一种方法来做我想做的事情。我有一个由时间戳索引的数据帧,我想将某个时段存储到一行中。以图形方式: # start date of the series start_date='20130101' # range of dates dates = pd.date_range(start_date, periods=6) # random dataframe df = pd.DataFrame(

我对pandas DataFrame相当陌生,但我一直在看教程和阅读有关它的文档,我无法完全想出一种方法来做我想做的事情。我有一个由时间戳索引的数据帧,我想将某个时段存储到一行中。以图形方式:

    # start date of the series
start_date='20130101'
# range of dates
dates = pd.date_range(start_date, periods=6)

# random dataframe
df = pd.DataFrame([["(1,1)","(1,2)"],
                   ["(2,1)","(2,2)"],
                   ["(3,1)","(3,2)"],
                   ["(4,1)","(4,2)"],
                   ["(5,1)","(5,2)"],
                   ["(6,1)","(6,2)"]], index=dates, columns=list('AB'))
print(df)
# range of bucketing periods, in this case I will get just three periods covering two days each
rng = pd.period_range(start_date, periods=3,freq='2D')
这导致

              A      B
2013-01-01  (1,1)  (1,2)
2013-01-02  (2,1)  (2,2)
2013-01-03  (3,1)  (3,2)
2013-01-04  (4,1)  (4,2)
2013-01-05  (5,1)  (5,2)
2013-01-06  (6,1)  (6,2)
我现在要做的是生成一个新的数据帧,其中我将
rng=pd.period\u范围(开始日期,periods=3,freq='2D')
中的句点作为索引,并将对应于该句点的行作为连续列:

              A      B      A1      B1
2013-01-01  (1,1)  (1,2)  (2,1)  (2,2)            
2013-01-03  (3,1)  (3,2)  (4,1)  (4,2)  
2013-01-05  (5,1)  (5,2)  (6,1)  (6,2)
Api中有什么方法可以用来实现这一点吗? 我想我还需要生成像A1、B1这样的新标签

另外,在我想了一点之后,我可能可以

              A      A1      B      B1
2013-01-01  (1,1)  (2,1)  (1,2)  (2,2)            
2013-01-03  (3,1)  (4,1)  (3,2)  (4,2)  
2013-01-05  (5,1)  (6,1)  (5,2)  (6,2)

其中一种方法是将时段转换为
timestamp
,并生成一个数据帧,然后用
ffill
方法将其浓缩为
NaN
,并将新的timestamp列设置为索引,即

n = pd.DataFrame(rng.to_timestamp()).set_index(rng.to_timestamp())

result = pd.concat([df, n], axis=1).fillna(method='ffill').set_index(0)

result = result.set_index(result.groupby(level=0).cumcount(), append=True).unstack()
输出

A B 0 1 0 1 0 2013-01-01 (1,1) (2,1) (1,2) (2,2) 2013-01-03 (3,1) (4,1) (3,2) (4,2) 2013-01-05 (5,1) (6,1) (5,2) (6,2) In [1024]: A B 0 1 0 1 0 2013-01-01 (1,1) (2,1) (1,2) (2,2) 2013-01-03 (3,1) (4,1) (3,2) (4,2) 2013-01-05 (5,1) (6,1) (5,2) (6,2) 在[1024]中:
如何确定周期?周期来自上文定义的周期范围rng。在这个例子中,我把它作为一个2d周期。我这样问是因为你想要的东西可以通过一个简单的整形来实现:
pd.DataFrame(np.reforme(df.values,(-1,4))
这是一种方法,谢谢。但是,我想根据时间戳和我生成的周期进行选择。这正是我想要的。非常感谢。很高兴能帮助@Paco