从 Pandas DataFrame 中的列创建一个元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32485488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:51:59  来源:igfitidea点击:

create a tuple from columns in a pandas DataFrame

pythonpandastuplesdataframe

提问by Nicole Goebel

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:

我想从 Pandas 数据帧中的列自动创建一个元组(将传递给 scipy.stats 函数),以便元组的每一行都是数据帧每一列的值。这是我的数据帧的标题:

                     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

I can achieve this manually like so:

我可以像这样手动实现:

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)

Thanks for your help!

谢谢你的帮助!

采纳答案by hellpanderr

What about

关于什么

tuple([tuple(df[col]) for col in df])

回答by EdChum

Just call applyand call tuple:

只需拨打apply电话tuple

In [3]:
df = pd.DataFrame(np.random.randn(5,3))
df

Out[3]:
          0         1         2
0  0.785562 -0.263813  2.239865
1  1.083918  0.035746  0.429111
2  1.422599 -0.818151  0.765725
3  1.022289  0.098561 -2.393095
4 -0.548451 -0.345796  0.298237

In [4]:
df.apply(tuple, axis=1)

Out[4]:
0     (0.785562108573, -0.263813112223, 2.23986497964)
1     (1.08391788685, 0.0357457180803, 0.429110675053)
2      (1.4225989372, -0.818150896781, 0.765724984713)
3     (1.02228880387, 0.0985610274998, -2.39309469576)
4    (-0.548450748411, -0.345796089243, 0.298237353...
dtype: object