Python pandas 使用可变数量的输入创建数据框 [英] Python pandas creating data frame with variable number of inputs

查看:97
本文介绍了Python pandas 使用可变数量的输入创建数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有可变列数的pandas数据框.

I am trying to create pandas dataframe with variable number of columns.

import pandas as pd
import numpy as np
MonthNumber = np.array([1,1,2,4,5,6,5])
Type1 = np.array(['A','B','C',A','A','B','C'])
Type2 = np.array([1,1,0,1,0,0,1])
Data = np.array([1.1,3,.52,34,15,45,34])
Data2 = Data * 1.1
Data3 = Data * 2 

def variableDataFrame(*args):
    df = pd.DataFrame({'MonthNumber':args1, 'Data':args2})
    print df.columns

因此,对函数的调用如下:

So, the calls to function looks like:

variableDataFrame(MonthNumber,Data) #prints MonthNumber,Data
variableDataFrame(MonthNumber,Type1,Data) # prints MonthNumber,Type1,Data

推荐答案

为了完成您的代码并使之适用于您所提供的代码,我将这样做:

In order to complete your code and make it work for what you've provided, I'd do this:

def variableDataFrame(*args):
    """I'm assuming the last argument is the data argument.
    Everything else gets put into an index.
    If you want these as columns instead of rows,
    put a ".T" at the end of the return argument."""

    return pd.DataFrame(args[-1], index=pd.MultiIndex.from_arrays(args[:-1]))

    # Optional return if you want columns instead
    # return pd.DataFrame(args[-1], index=pd.MultiIndex.from_arrays(args[:-1])).T

但是我会对此大加警告!!

您正在创建pd.DataFrame的替代品.您/我已经做的就是重新排列并混淆参数.为了使它正常工作,您需要知道什么时候在哪个插槽中进行操作,这与您一开始使用pd.DataFrame时需要了解的相同.

But I will put a large caveat on this!!!

You are creating a replacement for pd.DataFrame. All you've/I've done is reshuffle and obfuscate the arguments. In order to get it to work, you need to know what goes in what slot which is the same thing you need to know when you use pd.DataFrame in the first place.

有了这些,我希望对您有所帮助.

With that out of my system, I hope that helps.

这篇关于Python pandas 使用可变数量的输入创建数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆