Python 将一行追加N次到应用和追加中
我收到以下错误:Python 将一行追加N次到应用和追加中,python,pandas,dataframe,Python,Pandas,Dataframe,我收到以下错误: File "/packages/pandas/core/common.py", line 380, in _asarray_tuplesafe result[:] = [tuple(x) for x in values] TypeError: 'numpy.int64' object is not iterable 运行时: df.apply(lambda row: (df2.append(([row]*(row[6].split(',').__len__()))
File "/packages/pandas/core/common.py", line 380, in _asarray_tuplesafe
result[:] = [tuple(x) for x in values]
TypeError: 'numpy.int64' object is not iterable
运行时:
df.apply(lambda row: (df2.append(([row]*(row[6].split(',').__len__())), ignore_index=True)), axis=1)
目标是为每个原始数据帧行(df
)向空df(df2
)追加N次。其中N是特定字段行[6]
具有的值的数量
示例行用于df
:
Id|列表
0 | 126
1 | 126127304305
df2
应为:
Id|列表
0 | 126
1 | 126
1 | 127
1 | 304
1 | 305
如您所见,我尝试将该行作为列表发送,但两者都不起作用。有什么想法吗?这里是使用
df.List=df.List.str.split(',')
unnesting(df,['List'])
Out[466]:
List Id
0 126 0
1 126 1
1 127 1
1 304 1
1 305 1
def unnesting(df, explode):
idx = df.index.repeat(df[explode[0]].str.len())
df1 = pd.concat([
pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
df1.index = idx
return df1.join(df.drop(explode, 1), how='left')