python - 从数据框中访问项目并使用相同的内容创建一个新项目

标签 python pandas dataframe

我正在使用这个数据框 (df1):

df1 =  pd.read_csv('City_Zhvi_AllHomes.csv',header=None)

其内容包含以“年-月”(1-12) 格式编写的列。现在我必须创建一个新的 dataframe,其中包含来自 Df1 的数据,分为 4 个季度。这是我所做的:

Month = ['00','01','02','03','04','05','06','07','08','09','10','11','12']

Year=['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16']


for i in range(0,15):

      for j in range(0,12):

          newdf = pd.DataFrame[df1.loc[2,'20'+Year[i]+'-'+Month[j+1]] + df1.loc[2,'20'+Year[i]+'-'+Month[j+2]]+ df1.loc[2,'20'+Year[i]+'-'+Month[j+3]],columns = ['Q1','Q2','Q3','Q4'] ] 
          j = j + 3


print newdf . 

我专门为第二行执行此操作,但我需要对整个数据集执行此操作

现在我收到此错误:

'type' object has no attribute 'getitem'

这是示例数据:

0             CountyName  SizeRank  2000-01  2000-02  2000-03  2000-04   
1                 Queens         1      NaN      NaN      NaN      NaN   
2            Los Angeles         2   204400   207000   209800   212300   
3                   Cook         3   136800   138300   140100   141900   
4           Philadelphia         4    52700    53100    53200    53400   

newdf 应包含:

Q1                      
(204400+207000+209800)

同样,Q2、Q3、Q4

我该如何继续?

最佳答案

我认为你可以使用resample按列 axis=1 和聚合 sum,但首先是必要的 set_indexCountyNameSizeRank,然后首先转换列名称 to_datetime然后to_period :

#remove header=None for first row in csv as columns
df =  pd.read_csv('City_Zhvi_AllHomes.csv')

df = df.set_index(['CountyName','SizeRank'])
#with real data set index of all not dates columns
#df = df.set_index(['RegionID', 'RegionName', 'State', 'Metro', 'CountyName', 'SizeRank'])
df.columns = pd.to_datetime(df.columns).to_period('M')
print (df)
                        2000-01   2000-02   2000-03   2000-04
CountyName   SizeRank                                        
Queens       1              NaN       NaN       NaN       NaN
Los Angeles  2         204400.0  207000.0  209800.0  212300.0
Cook         3         136800.0  138300.0  140100.0  141900.0
Philadelphia 4          52700.0   53100.0   53200.0   53400.0

print (df.columns)
PeriodIndex(['2000-01', '2000-02', '2000-03', '2000-04'], dtype='period[M]', freq='M')

df = df.resample('Q', axis=1).sum()
print (df)
                         2000Q1    2000Q2
CountyName   SizeRank                    
Queens       1              NaN       NaN
Los Angeles  2         621200.0  212300.0
Cook         3         415200.0  141900.0
Philadelphia 4         159000.0   53400.0

关于python - 从数据框中访问项目并使用相同的内容创建一个新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41822997/

相关文章:

python - 当跨模块派生类时,名称解析如何工作?

python - 你真的可以使用 Visual Studio 2008 IDE 来编写 Python 代码吗?

python - python2 和 python3 之间的执行时间略有不同

python - MemoryError : Unable to allocate array with shape (118, 840983) 和数据类型 float64

python - 按数据框中的对象属性分组

python - 如何用python提高置换算法效率

python - Pandas:数据框与用于定义它的 numpy.array 之间的关系

python - Pandas 为每个唯一 ID 选择较高的值

dataframe - 将 Symbol 参数传递给 @where 以使用 Julia 选择 DataFrame 的行

python - 将文本表转换为 Pandas 数据框