【问题标题】:Creating arrays quickly from a dataframe with lots of values?从具有大量值的数据框中快速创建数组?
【发布时间】:2017-09-14 10:55:48
【问题描述】:
  1. 我有一个包含大量值(259 行 × 27 列)的大型数据框(通过 pandas 从 csv 文件导入)。该指数是从 1996 年 1 月到 2017 年 7 月的月份。

Image of my dataframe

  1. 我想按年份对每一列进行排序,例如K37L:1996、1997、1998、1999、2000等; K37M:1996、1997、1998、1999、2000 等。

  2. 这是我当前的代码:

#Importing CSV
import pandas as pd
import numpy as np
df = pd.read_csv('file.csv', index_col=0, skipinitialspace=True)

#Calling a column
K37L = df['K37L']

#Filtering this column by year (from 1996 to 2017)
K37L96 = K37L.filter(regex = '1996', axis = 0); npK37L96 = np.array(K37L96)
...
...
...
K37L17 = K37L.filter(regex = '2017', axis = 0); npK37L17 = np.array(K37L17)
  1. 这会产生我想要的:K37L filtered by 1996

但是,这是一个乏味的过程,因为我必须输入所有年份和列名才能得到我想要的,这需要很长时间。有没有更快/更优雅的方法来做到这一点?

编辑:这是要求的 df.head() 输出:

          K37L  K37M  K37N  K37P  K37Q  K37R  K37S  K37T  K37U  K37V  ...   \
1996 Jan  78.9  79.4  71.7  36.7   0.0  88.7  94.1  90.7  80.2  98.9  ...    
1996 Feb  79.3  81.0  72.7  36.7   0.0  88.7  94.3  90.9  79.8  98.7  ...    
1996 Mar  79.8  80.4  72.7  36.7   0.0  89.0  94.6  91.0  79.6  98.6  ...    
1996 Apr  80.4  80.7  72.9  36.7   0.0  89.0  94.6  91.3  79.2  97.9  ...    
1996 May  80.6  80.7  72.9  36.7   0.0  89.1  94.7  91.9  79.2  96.6  ...    

          K385  K386   K387  K388  K389  K38A  K38B  K38C  K38D  K38E  
1996 Jan  70.9  78.7  257.8  83.9  79.7  92.2  73.8  86.4  79.6  74.0  
1996 Feb  70.7  78.7  257.2  83.9  79.8  92.6  73.7  86.6  79.9  73.9  
1996 Mar  70.9  78.7  257.3  83.9  80.1  92.6  73.8  87.2  80.1  74.0  
1996 Apr  70.8  78.9  256.6  83.9  80.4  92.7  73.9  87.9  80.7  74.0  
1996 May  70.9  78.9  256.3  83.9  80.5  92.9  73.9  88.0  80.7  74.1  

[5 rows x 27 columns]

【问题讨论】:

  • 请不要发布您的数据图片。只需将df.head() 的输出复制粘贴为原始文本...
  • 抱歉,我已将 df.head() 输出添加到主要问题中

标签: python python-3.x pandas numpy dataframe


【解决方案1】:

你可以使用:

np.random.seed(458)
cols = ['K37L', 'K37M', 'K37N', 'K37P', 'K37Q', 'K37R', 'K37S', 'K37T', 'K37U','K37V', 'K37W', 'K37X', 'K37Y', 'K37Z', 'K382', 'K383', 'K384', 'K385', 'K386', 'K387', 'K388', 'K389', 'K38A', 'K38B', 'K38C', 'K38D', 'K38E']
idx = pd.date_range('1996-01-01', periods=259, freq='MS').strftime('%Y %b')
df = pd.DataFrame(np.random.randint(20, size=(259,27)), index=idx, columns=cols)
print (df.head(3))
          K37L  K37M  K37N  K37P  K37Q  K37R  K37S  K37T  K37U  K37V  ...   \
1996 Jan     8    13    18     1     6     2     1    11    13     0  ...    
1996 Feb    12     0    14     0    11     0     1    10     3     4  ...    
1996 Mar     5     8     8     8     5     5     2     8     1     7  ...    

          K385  K386  K387  K388  K389  K38A  K38B  K38C  K38D  K38E  
1996 Jan    18    16     0    11    18    18    11    18    11    17  
1996 Feb     9    12    15     7     7     0    17     3     6     7  
1996 Mar    13     9     0     9     2    17    13     1    12     9  

[3 rows x 27 columns]

由to_datetime创建Datetimeindex:

df.index = pd.to_datetime(df.index, format='%Y %b')
print (df.head(3))
            K37L  K37M  K37N  K37P  K37Q  K37R  K37S  K37T  K37U  K37V  ...   \
1996-01-01     8    13    18     1     6     2     1    11    13     0  ...    
1996-02-01    12     0    14     0    11     0     1    10     3     4  ...    
1996-03-01     5     8     8     8     5     5     2     8     1     7  ...    

            K385  K386  K387  K388  K389  K38A  K38B  K38C  K38D  K38E  
1996-01-01    18    16     0    11    18    18    11    18    11    17  
1996-02-01     9    12    15     7     7     0    17     3     6     7  
1996-03-01    13     9     0     9     2    17    13     1    12     9  

[3 rows x 27 columns]

所以对于按年选择使用partial string indexing 和对于选择列[](相同的语法):

#seelcting rows with year 2000
print (df['2000'])
            K37L  K37M  K37N  K37P  K37Q  K37R  K37S  K37T  K37U  K37V  ...   
2000-01-01    12    15     8    14     2     0    17     0     8    14  ...    
2000-02-01    14    10    11     4    18     1     3    12     9    11  ...    
2000-03-01     4     5    17    16    13     6    18     6    12    12  ...    
2000-04-01     2    15     3     5     6     6    17     3     1     3  ...    
2000-05-01     6    14    14     9     4     0     4    10    14    15  ...    


#selecting column K37P
print (df['K37P'])
1996-01-01     1
1996-02-01     0
1996-03-01     8
1996-04-01    11
1996-05-01    14
1996-06-01    12
1996-07-01    12
1996-08-01    14
1996-09-01     2
1996-10-01     1

先选择列再选择年份:

print (df['K37L']['2000'])
2000-01-01    12
2000-02-01    14
2000-03-01     4
2000-04-01     2
2000-05-01     6
2000-06-01    10
2000-07-01     2
2000-08-01    13
2000-09-01    18
2000-10-01     4
2000-11-01    12
2000-12-01    11
Name: K37L, dtype: int32

对于 numpy 数组使用:

print (df['K37L']['2000'].values)
[12 14  4  2  6 10  2 13 18  4 12 11]

如果需要按年份排列的字典:

然后通过partial string indexing选择years,最后通过values转换为数组dictionary:

d = {x: df[str(x)].values for x in range(1996, 2018)}

print (d[2000])
[[12 15  8 14  2  0 17  0  8 14 17 15  2  3 14 17 19  2  8  7  5  7 12 13
  17  7  4]
 [14 10 11  4 18  1  3 12  9 11  8  3 12 19 19 15  7 19 14 12  5 19 14 15
   7 11  7]
 [ 4  5 17 16 13  6 18  6 12 12  7 15  3 16  2 18 14 18 15  8  5  9  3  7

【讨论】:

  • 好的,我刚试过,这就是我得到的:i.imgur.com/AfgZK0j.png 我不确定这是否正确。我可以看到月份和年份之间存在某种分离,这很好,但是我不明白所有这些标签的含义
  • 好的,你需要2d数组吗?
  • 是的,我确实这样做了!是否有可能从中得到一个二维数组?
  • 我创建答案,这是你想要的吗?
  • 很不错的jezrael,但是可以按年份过滤吗?哦,对不起,我没有阅读接下来的步骤!让我试试,我会回复你的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2021-07-26
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多