从pandas DataFrame中的列创建元组 [英] create a tuple from columns in a pandas DataFrame
本文介绍了从pandas DataFrame中的列创建元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从pandas数据框中的列自动创建一个元组(传递给scipy.stats函数),以便该元组的每一行都是该数据帧中每一列的值。
这是我数据框的标题:
I would like to automatically create a tuple (to be passed to a scipy.stats function) from columns in a pandas dataframe, so that each row of the tuple are the values from each column of the dataframe. here is the header from my dataframe:
4_3-a-0 5_3-a-4 7_3-a-3
datetime_pac
2015-09-03 22:00:00 -100.4 -96.857143 -55.000000
2015-09-03 22:01:00 -100.5 -91.700000 -55.600000
2015-09-03 22:02:00 -100.4 -90.875000 -55.900000
2015-09-03 22:03:00 -100.4 -94.000000 -55.555556
2015-09-03 22:04:00 -100.5 -99.500000 -55.545455
我可以这样手动实现:
from scipy import stats
stats.f_oneway(df.ix[:,0], df.ix[:,1], df.ix[:,2])
但是在以下情况下,我想自动化它:数据框中的列是未知的。以下尝试(及其多种变体)将不起作用:
But I would like to 'automate' it in cases where the number of columns in the dataframe is unknown. The following attempts (and many variations of) would not work:
stats.f_oneway(tuple(x) for x in xtmp.values)
stats.f_oneway((xtmp[x]) for x in xtmp.columns)
感谢您的帮助!
推荐答案
怎么样
tuple([tuple(df[col]) for col in df])
这篇关于从pandas DataFrame中的列创建元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文