【发布时间】:2016-02-18 09:40:42
【问题描述】:
当我想从另一列的拆分中创建一个新列时,我的 assign 方法有问题。如果我选择 split 方法的值,我会得到错误 ValueError: Length of values does not match length of index.如果我只是应用拆分,而不选择(索引)任何值,我会得到一个包含列表的新列。
如果我不索引 split 方法的输出,这里是输出
(
pd.DataFrame({
"Gene": ["G1", "G1", "G2", "G2"],
"Sample": ["H1_T1", "H2_T1", "H1_T1", "H2_T1"]
})
.assign(Timepoint = lambda x: x.Sample.str.split("_")[1])
)
Gene Sample Timepoint
0 G1 H1_T1 [H1, T1]
1 G1 H2_T1 [H2, T1]
2 G2 H1_T1 [H1, T1]
3 G2 H2_T1 [H2, T1]
这是一个示例,我想从 Sample 列中选择 T1 或 T2 值并给出错误:
(
pd.DataFrame({
"Gene": ["G1", "G1", "G2", "G2"],
"Sample": ["H1_T1", "H2_T1", "H1_T1", "H2_T1"]
})
.assign(Timepoint = lambda x: x.Sample.str.split("_")[1])
)
我从中得到的错误是:
/home/user/anaconda3/lib/python3.4/site-packages/pandas/core/series.py in _sanitize_index(data, index, copy)
2739
2740 if len(data) != len(index):
-> 2741 raise ValueError('Length of values does not match length of '
2742 'index')
2743
ValueError: Length of values does not match length of index
【问题讨论】:
标签: python pandas assign method-chaining