【问题标题】:Using Pandas dataframe with FOR loops使用带有 FOR 循环的 Pandas 数据框
【发布时间】:2014-04-03 20:21:46
【问题描述】:

感谢您的关注。

我正在尝试修改 Python 脚本以从网站下载大量数据。我已经决定考虑到将要使用的大量数据,我想为此将脚本转换为 Pandas。到目前为止我有这个代码。

snames = ['Index','Node_ID','Node','Id','Name','Tag','Datatype','Engine']
sensorinfo = pd.read_csv(sensorpath, header = None, names = snames, index_col=['Node', 'Index'])
for j in sensorinfo['Node']:    
     for z in sensorinfo['Index']:

    # create a string for the url of the data
    data_url = "http://www.mywebsite.com/emoncms/feed/data.json?id=" + sensorinfo['Id'] + "&apikey1f8&start=&end=&dp=600"
    print data_url
    # read in the data from emoncms
    sock = urllib.urlopen(data_url)
    data_str = sock.read()
    sock.close

    # data is output as a string so we convert it to a list of lists
    data_list = eval(data_str)
    myfile = open(feed_list['Name'[k]] + ".csv",'wb')

    wr=csv.writer(myfile,quoting=csv.QUOTE_ALL)

代码的第一部分给了我一个非常漂亮的表格,这意味着我正在打开我的 csv 数据文件并导入信息,我的问题是:

所以我想用伪代码来做这个:

For node is nodes (4 nodes so far)
 For index in indexes
       data_url = websiteinfo + Id + sampleinformation
      smalldata.read.csv(data_url)
      merge(bigdata, smalldata.no_time_column)

这是我在这里的第一篇文章,我尽量保持简短,但仍提供相关数据。如果我需要澄清任何事情,请告诉我。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    在你的伪代码中,你可以这样做:

    dfs = []
    For node is nodes (4 nodes so far)
       For index in indexes
          data_url = websiteinfo + Id + sampleinformation
          df = smalldata.read.csv(data_url)
          dfs.append(df)
    
    df = pd.concat(dfs)
    

    【讨论】:

    • 因为他的代码中缺少必要的步骤并且他使用了伪代码。
    • 如果他给出完整的功能,我很乐意给他写
    • 我试图包含我认为可以让我的观点得到理解的步骤,而不会因我的整个脚本而陷入困境。您希望看到哪些重要步骤?
    • 说实话,不清楚您要做什么。我的列表解决方案对您有用吗?
    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 2018-10-15
    • 2020-03-01
    • 1970-01-01
    • 2014-04-28
    • 2022-01-20
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多