Python,如何生成第一列+第二列值之一的新行?
Python, How to generate new rows with first column + one of value in second column?
如何使用第一列 + 第二列中的一个值生成新行?
I have CSV or TXT dataset such as:
1 [6377, 7153]
2 [22, 33, ........]
3 [132, .......]
4 [1, ......]
I would like to generate new rows which are the first column + one of the value in the second column like:
1 6377
1 7153
2 22
2 33
2 ...
2 ...
3 132
3 ...
4 1
4 ...
您可以使用 df.explode()。它将类似元素的列表转换为行
array1 = {'col': [['1223', '4233'], ['5343','343434']]}
df= pd.DataFrame(array1)
print(df)
#output
col
0 [1223, 4233]
1 [5343, 343434]
df.explode('col')
#output
col
0 1223
0 4233
1 5343
1 343434
如何使用第一列 + 第二列中的一个值生成新行?
I have CSV or TXT dataset such as:
1 [6377, 7153]
2 [22, 33, ........]
3 [132, .......]
4 [1, ......]
I would like to generate new rows which are the first column + one of the value in the second column like:
1 6377
1 7153
2 22
2 33
2 ...
2 ...
3 132
3 ...
4 1
4 ...
您可以使用 df.explode()。它将类似元素的列表转换为行
array1 = {'col': [['1223', '4233'], ['5343','343434']]}
df= pd.DataFrame(array1)
print(df)
#output
col
0 [1223, 4233]
1 [5343, 343434]
df.explode('col')
#output
col
0 1223
0 4233
1 5343
1 343434