python如何定义一个数组接收数据,如何在Python中创建数据框数组

I want to write a piece of code to create multiple arrays of dataFrames with their names in the format of word_0000, where the four digits are month and year.

An example of what I'd like to do is to create the following dataframes:

df_0115, df_0215, df_0315, ... , df_1215

stat_0115, stat_0215, stat_0315, ... , stat_1215

解决方案

I suggest that you create a dictionary to hold the DataFrames. That way you will be able to index them with a month-day key:

import datetime as dt

import numpy as np

import pandas as pd

dates_list = [dt.datetime(2015,11,i+1) for i in range(3)]

month_day_list = [d.strftime("%m%d") for d in dates_list]

dataframe_collection = {}

for month_day in month_day_list:

new_data = np.random.rand(3,3)

dataframe_collection[month_day] = pd.DataFrame(new_data, columns=["one", "two", "three"])

for key in dataframe_collection.keys():

print("\n" +"="*40)

print(key)

print("-"*40)

print(dataframe_collection[key])

The code above prints out the following result:

========================================

1102

----------------------------------------

one two three

0 0.896120 0.742575 0.394026

1 0.414110 0.511570 0.268268

2 0.132031 0.142552 0.074510

========================================

1103

----------------------------------------

one two three

0 0.558303 0.259172 0.373240

1 0.726139 0.283530 0.378284

2 0.776430 0.243089 0.283144

========================================

1101

----------------------------------------

one two three

0 0.849145 0.198028 0.067342

1 0.620820 0.115759 0.809420

2 0.997878 0.884883 0.104158

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值