Reputation: 3163
I'm receiving the following error:
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
When running:
df.apply(lambda row: (df2.append(([row]*(row[6].split(',').__len__())), ignore_index=True)), axis=1)
The goal is to apply for each origin dataframe row (df
) an append N times to an empty df (df2
). Where N is the number of values that the specific field row[6]
has.
Rows of example are for the df
:
Id|List
0|126
1|126,127,304,305
The df2
should be:
Id|List
0|126
1|126
1|127
1|304
1|305
As you can see I tried sending the row as a list, but don't work neither. Any idea?
Upvotes: 1
Views: 64
Reputation: 323326
Here is one way after split
using unnesting
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')
Upvotes: 1