如何从另一个数据框创建新的数据框?

时间:2019-06-09 07:09:59

标签: python pandas dataframe

我有一个数据框df2

           extention  just_dates  
0     d875679ds1.htm  2003-01-02  
1     d875679ds1.htm  2015-10-31  
2       form_s1a.htm  2015-11-01  
3       form_s1a.htm  2015-11-01  
4       form_s1a.htm  2015-11-01  
5       form_s1a.htm  2015-11-01  
6    d698362ds1a.htm  2015-11-01  
7    d698362ds1a.htm  2015-11-01  
8       form_s1a.htm  2015-11-01  
9       form_s1a.htm  2015-11-01  
10      form_s1a.htm  2015-11-01  
11   d698362ds1a.htm  2015-11-01  
12      form_s1a.htm  2015-11-01  
13      form_s1a.htm  2015-11-01  
14      form_s1a.htm  2015-11-01  
15   d804420ds1a.htm  2015-11-01  
16   d923792ds1a.htm  2015-11-02  
17   d923792ds1a.htm  2015-11-02  
18   d923792ds1a.htm  2015-11-02  
19  a2221572zs-1.htm  2015-11-02  
20    d938556df1.htm  2015-11-02  
21    d938556df1.htm  2015-11-02  
22    d938556df1.htm  2015-11-02  
23    d938556df1.htm  2015-11-02  
24    d766811ds1.htm  2015-11-02  
25     d44564d8k.htm  2015-11-02  
26   d776249ds1a.htm  2015-11-02  
27   d776249ds1a.htm  2015-11-02  
28   d776249ds1a.htm  2015-11-02  
29   d776249ds1a.htm  2015-11-02  
30   d776249ds1a.htm  2015-11-02  
31   d776249ds1a.htm  2015-11-02  
32   d776249ds1a.htm  2015-11-02  
33   d776249ds1a.htm  2015-11-03  
34   d776249ds1a.htm  2015-11-03  
35   d776249ds1a.htm  2015-11-03  
36   d938481ds1a.htm  2015-11-03  
37    d766811ds1.htm  2015-11-03  
38   d938481ds1a.htm  2015-11-03  
39   d938481ds1a.htm  2015-11-03  
40   d938481ds1a.htm  2015-11-03  
41   d938481ds1a.htm  2015-11-03  
42   d938481ds1a.htm  2015-11-03  
43    d766811ds1.htm  2015-11-03  
44    d946612ds1.htm  2015-11-04  
45      forms-1a.htm  2015-11-04  
46      forms-1a.htm  2015-11-04 

使用命令

out=[]
out.append(df2['just_dates'].value_counts().sort_index())

我成为

2003-01-02     1
2015-10-31     1
2015-11-01    14
2015-11-02    17
2015-11-03    11
2015-11-04     3

我到底想要什么。它统计数据帧df2中每天的条目。但是我的问题是我想拥有一个新的数据框out,而我认为out不是一个数据框,对吗?我认为这是因为我没有标题,也没有行号。我怎样做才能成为一个新的数据帧out

1 个答案:

答案 0 :(得分:0)

您的输出是pd.series,您可以使用to_frame()将其转换为df

out = df2['just_dates'].value_counts().sort_index().to_frame()

例如:

d = {'col1': [1, 2,3,3,5,4,5], 'col2': [3, 4,5,4,6,6,7]}
df = pd.DataFrame(data=d)
df

输出:

  col1 col2
0   1   3
1   2   4
2   3   5
3   3   4
4   5   6
5   4   6
6   5   7

然后

   df['col1'].value_counts().sort_index().to_frame()

输出:

    col1
1   1
2   1
3   2
4   1
5   2
相关问题