我有一个pandas数据框,其中一串文本字符串包含换行符。 我想拆分每个CSV字段,并为每个条目创建一个新行。
我的数据框就像:
Col-1 Col-2
A Notifications
Returning Value
Both
B mine
Why Not?
预期输出为:
Col-1 Col-2
A Notifications
A Returning Value
A Both
B mine
B Why Not?
答案 0 :(得分:1)
第一个replace()
字符串''
和np.nan
,然后使用fillna(method='ffill')
:
df = pd.DataFrame({'Col-1':['A','','','B',''],
'Col-2':['Notifications','Returning Value','Both','mine','Why Not?']})
df
Col-1 Col-2
0 A Notifications
1 Returning Value
2 Both
3 B mine
4 Why Not?
df['Col-1'] = df['Col-1'].replace('',np.nan).fillna(method='ffill')
df
Col-1 Col-2
0 A Notifications
1 A Returning Value
2 A Both
3 B mine
4 B Why Not?
答案 1 :(得分:1)
重建第二列以展平系列,然后将其与第一列连接起来
df = pd.DataFrame({'Col-1': ['A', 'B'], 'Col-2': ['Notifications\nReturning Value\nBoth', 'mine\nWhy Not?']})
df
表示形式:
Col-1 Col-2
0 A Notifications\nReturning Value\nBoth
1 B mine\nWhy Not?
主要部分:
series = pd.DataFrame(df['Col-2'].str.split('\n').tolist()).stack()
series.index = series.index.droplevel(1)
series.name = 'Col-2'
result = pd.concat([df['Col-1'], series], axis=1)
结果:
Col-1 Col-2
0 A Notifications
1 A Returning Value
2 A Both
3 B mine
4 B Why Not?
答案 2 :(得分:0)
您想要的pd.reset_index()
假设您的数据存储在名为df的变量中:
df = df.reset_index().set_index('Col-1')
一个虚拟的示例,因为您没有提供创建MultiIndex的简便方法:
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
First second
bar one 0.792900
two -0.070508
baz one -0.599464
two 0.334504
foo one 0.835464
two 1.614845
qux one 0.674623
two 1.907550
现在,如果我们希望第一列成为索引:
s = s.reset_index().set_index('first')
print(s)
second 0
first
bar one 0.792900
bar two -0.070508
baz one -0.599464
baz two 0.334504
foo one 0.835464
foo two 1.614845
qux one 0.674623
qux two 1.907550
此处有更多信息:Advanced Indexing