拆分数据框的数据框并插入一列

时间:2018-12-03 08:28:07

标签: python pandas dataframe

我有大量带日期标记的文件,其文件名是日期。每个文件都有一组dataframe中具有相同结构的记录。

我想将它们做成一个dataframe,文件名作为一列。

示例代码如下。

import pandas as pd
import numpy as np
df1 = pd.DataFrame({
    'a': np.random.rand(6),
    'b': np.random.choice([4,9,np.nan], 6),
})

df2 = df1*3
df3 = np.sqrt(df2)
df4 = df1*df3
df5 = df2*0.5
df6 = df2*df2

df = pd.DataFrame({
    'fname': pd.date_range('1/1/2018', periods=6, freq='D'),
    'df': [df1, df2, df3, df4, df5, df6]
})

预期输出df为:

    a           b   fname
0   0.634776    NaN 2018-01-01
1   0.616349    NaN 2018-01-02
2   0.502559    NaN 2018-01-03
3   0.545378    4.0 2018-01-04
4   0.658668    4.0 2018-01-05
5   0.822417    9.0 2018-01-06
0   1.904328    NaN 2018-01-01
1   1.849046    NaN 2018-01-02
2   1.507678    NaN 2018-01-03
3   1.636133    2.0 2018-01-04
4   1.976004    2.0 2018-01-05
5   2.467252    3.0 2018-01-06
...and so on...

2 个答案:

答案 0 :(得分:1)

我认为最好创建dict,因此可以使用concat

d = dict(zip(pd.date_range('1/1/2018', periods=6, freq='D'),
              [df1, df2, df3, df4, df5, df6]))

df = (pd.concat(d)
       .reset_index(level=1, drop=True)
       .rename_axis('fname')
       .reset_index()
       )

或通过参数concat将数据帧列表传递到keys

df = (pd.concat([df1, df2, df3, df4, df5, df6], 
               keys=pd.date_range('1/1/2018', periods=6, freq='D'))
       .reset_index(level=1, drop=True)
       .rename_axis('fname')
       .reset_index()
       )

print (df.head(10))
       fname         a     b
0 2018-01-01  0.075056   9.0
1 2018-01-01  0.646019   9.0
2 2018-01-01  0.656301   NaN
3 2018-01-01  0.420031   4.0
4 2018-01-01  0.239017   9.0
5 2018-01-01  0.921222   NaN
6 2018-01-02  0.225168  27.0
7 2018-01-02  1.938057  27.0
8 2018-01-02  1.968903   NaN
9 2018-01-02  1.260093  12.0

答案 1 :(得分:0)

要把数据帧放在一起,我认为您应该使用func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if collectionView == filterCollectionView { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterSelectionCollectionViewCell", for: indexPath) as! FilterSelectionCollectionViewCell cell.title.text = "Newest" //this is for the "Newest" cell. Of curse you should set the proper title for each indexPath cell.setNeedsLayout() cell.layoutIfNeede() return CGSize(width: cell.contenView.frame.width , height: cell.contentView.frame.height) } else { return CGSize(width: 10, height: 10) } } https://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html

您使用什么代码读取数据? 如果是这样的话

pd.Concat

file_name [:10]根据字符量从文件中获取日期