pandas 从列中可用的列表数据扩展行 [英] Pandas expand rows from list data available in column
本文介绍了 pandas 从列中可用的列表数据扩展行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在熊猫中有一个像这样的数据框:
I have a data frame like this in pandas:
column1 column2
[a,b,c] 1
[d,e,f] 2
[g,h,i] 3
预期输出:
column1 column2
a 1
b 1
c 1
d 2
e 2
f 2
g 3
h 3
i 3
如何处理此数据?
推荐答案
You can create DataFrame
by its constructor and stack
:
df2 = pd.DataFrame(df.column1.tolist(), index=df.column2)
.stack()
.reset_index(level=1, drop=True)
.reset_index(name='column1')[['column1','column2']]
print (df2)
column1 column2
0 a 1
1 b 1
2 c 1
3 d 2
4 e 2
5 f 2
6 g 3
7 h 3
8 i 3
如果需要按子集[['column1','column2']]
进行更改排序,则还可以省略第一个reset_index
:
If need change ordering by subset [['column1','column2']]
, you can also omit first reset_index
:
df2 = pd.DataFrame(df.column1.tolist(), index=df.column2)
.stack()
.reset_index(name='column1')[['column1','column2']]
print (df2)
column1 column2
0 a 1
1 b 1
2 c 1
3 d 2
4 e 2
5 f 2
6 g 3
7 h 3
8 i 3
另一种解决方案 DataFrame.from_records
从第一列创建DataFrame
,然后通过Series > stack
和 join
转换为原始的DataFrame
:
Another solution DataFrame.from_records
for creating DataFrame
from first column, then create Series
by stack
and join
to original DataFrame
:
df = pd.DataFrame({'column1': [['a','b','c'],['d','e','f'],['g','h','i']],
'column2':[1,2,3]})
a = pd.DataFrame.from_records(df.column1.tolist())
.stack()
.reset_index(level=1, drop=True)
.rename('column1')
print (a)
0 a
0 b
0 c
1 d
1 e
1 f
2 g
2 h
2 i
Name: column1, dtype: object
print (df.drop('column1', axis=1)
.join(a)
.reset_index(drop=True)[['column1','column2']])
column1 column2
0 a 1
1 b 1
2 c 1
3 d 2
4 e 2
5 f 2
6 g 3
7 h 3
8 i 3
这篇关于 pandas 从列中可用的列表数据扩展行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文