df.explode() 函数在 python 中不起作用
df.explode() function not working in python
我遇到了一个奇怪的问题,我在数据框中有一个列名 'window',它有一个值列表,即 [3,9,45,78]。我正在尝试使用 df.explode('window') 展开此列,但这没有任何作用。 'window' 列的数据类型是对象。
我检查了我的 pandas 版本 - 1.3.4
dataframe example
请记住,df.explode
并没有就地完成它的工作。它 RETURNS 一个新的数据框,其中包含以下更改:
import pandas as pd
data = [
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ]
]
df = pd.DataFrame( data, columns=['one', 'two','three','four'] )
print(df)
df2 = df.explode('two')
print(df2)
输出:
one two three four
0 0 [1, 2, 3] [4, 5, 6] 7
1 0 [1, 2, 3] [4, 5, 6] 7
2 0 [1, 2, 3] [4, 5, 6] 7
3 0 [1, 2, 3] [4, 5, 6] 7
4 0 [1, 2, 3] [4, 5, 6] 7
one two three four
0 0 1 [4, 5, 6] 7
0 0 2 [4, 5, 6] 7
0 0 3 [4, 5, 6] 7
1 0 1 [4, 5, 6] 7
1 0 2 [4, 5, 6] 7
1 0 3 [4, 5, 6] 7
2 0 1 [4, 5, 6] 7
2 0 2 [4, 5, 6] 7
2 0 3 [4, 5, 6] 7
3 0 1 [4, 5, 6] 7
3 0 2 [4, 5, 6] 7
3 0 3 [4, 5, 6] 7
4 0 1 [4, 5, 6] 7
4 0 2 [4, 5, 6] 7
4 0 3 [4, 5, 6] 7
我遇到了一个奇怪的问题,我在数据框中有一个列名 'window',它有一个值列表,即 [3,9,45,78]。我正在尝试使用 df.explode('window') 展开此列,但这没有任何作用。 'window' 列的数据类型是对象。 我检查了我的 pandas 版本 - 1.3.4
dataframe example
请记住,df.explode
并没有就地完成它的工作。它 RETURNS 一个新的数据框,其中包含以下更改:
import pandas as pd
data = [
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ],
[ 0, [1,2,3], [4,5,6], 7 ]
]
df = pd.DataFrame( data, columns=['one', 'two','three','four'] )
print(df)
df2 = df.explode('two')
print(df2)
输出:
one two three four
0 0 [1, 2, 3] [4, 5, 6] 7
1 0 [1, 2, 3] [4, 5, 6] 7
2 0 [1, 2, 3] [4, 5, 6] 7
3 0 [1, 2, 3] [4, 5, 6] 7
4 0 [1, 2, 3] [4, 5, 6] 7
one two three four
0 0 1 [4, 5, 6] 7
0 0 2 [4, 5, 6] 7
0 0 3 [4, 5, 6] 7
1 0 1 [4, 5, 6] 7
1 0 2 [4, 5, 6] 7
1 0 3 [4, 5, 6] 7
2 0 1 [4, 5, 6] 7
2 0 2 [4, 5, 6] 7
2 0 3 [4, 5, 6] 7
3 0 1 [4, 5, 6] 7
3 0 2 [4, 5, 6] 7
3 0 3 [4, 5, 6] 7
4 0 1 [4, 5, 6] 7
4 0 2 [4, 5, 6] 7
4 0 3 [4, 5, 6] 7