如何从另一个dataframe创建新的dataframe?

2024-04-26 07:38:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧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?你知道吗


Tags: 数据formoutjustdatesdf2htms1a
1条回答
网友
1楼 · 发布于 2024-04-26 07:38:52

您的输出是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

相关问题 更多 >