繁体   English   中英

如何将熊猫数据框字符串条目拆分为单独的行?

[英]How to Split pandas data frame string entry to separate rows?

我有一个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?

首先用np.nan replace()字符串'' ,然后使用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?

重建第二列以展平序列,然后将其与第一列连接:

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?

您想要的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

此处更多信息: 高级索引

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM