如何将系列分为两列 pandas [英] How to split series in two columns pandas

查看:83
本文介绍了如何将系列分为两列 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个充满数据的列表,我想将它们连接起来以创建一个数据框

I have three lists filled with data and I would like to concatenate them to create a dataframe

type of data_activationsLV : list
type of data_activationsF : list
type of data_activationsPC : list

三个列表的数据结构:

data_activationsLV     data_activationsF       data_activationsPC
    index    a         index     b              index     c
    14468    7.8       14468     7.2            14468     7.6         
    14469    7.8       14469     7.1            14469     7.0
    14470    7.9       14470     7.9            14470     8.1
    14471    8.2       14471     9.5            14471     9.9

..

我将它们转换为序列并将它们连接起来:

I transform them into series and concate them :

df15LV = pd.Series(data_activationsLV)
df15F = pd.Series(data_activationsF)
df15PC = pd.Series(data_activationsPC)

dfnew2=pd.concat([df15LV,df15F,df15PC], ignore_index=True, axis=1)

在这里,我有一个问题,在每一列中,它都会考虑旧列的名称及其带有值的索引

by cons here I have a problem, in each column, it considers the name of the old column and its index with the value

index    0              1                2
0        a14468  7.8    b14468   7.2     c14468   7.6
1        a14469  7.8    b14469   7.1     c14469   7.0
2        a14470  7.9    b14470   7.9     c14470   8.1
3        a14471  8.2    b14471   9.5     c14471   9.9

所以我测试了split函数:

So I tested the split function :

dfnew2['a'] = dfnew2[2].split(' ')

但是它不起作用,当我尝试拆分这些列时,会发生以下情况:

But it did not work,When I try to split those columns, this is what happens:

AttributeError: 'Series' object has no attribute 'split'

是否可能只有每一列的值:

Is it possible to have only the values for each columns:

index    df15LV     df15F      df15PC
0        7.8        7.2        7.6
1        7.8        7.1        7.0
2        7.9        7.9        8.1
3        8.2        9.5        9.9

推荐答案

我认为您需要使用

I think you need for splitting apply with str.split and selecting with str[1]:

print (data_activationsLV)
['14468  7.8', '14469  7.8']

print (data_activationsF)
['14468  7.2', '14469  7.1', '14470  7.9', '14471  9.5']

print (data_activationsPC)
['14468  7.6', '14470  8.1', '14471  9.9']

df15LV = pd.Series(data_activationsLV)
df15F = pd.Series(data_activationsF)
df15PC = pd.Series(data_activationsPC)

dfnew2=pd.concat([df15LV,df15F,df15PC], axis=1)
dfnew2 = dfnew2.apply(lambda x: x.str.split().str[1])
#if necessary convert to float
dfnew2 = dfnew2.astype(float)
print (dfnew2)
     0    1    2
0  7.8  7.2  7.6
1  7.8  7.1  8.1
2  NaN  7.9  9.9
3  NaN  9.5  NaN

另一种解决方案是使用list comprehension进行拆分:

Another solution is use list comprehension for splitting:

print (data_activationsLV)
['7.8', '7.8']
print (data_activationsF)
['7.2', '7.1', '7.9', '9.5']

print (data_activationsPC)
['7.6', '8.1', '9.9']

df15LV = pd.Series(data_activationsLV)
df15F = pd.Series(data_activationsF)
df15PC = pd.Series(data_activationsPC)

dfnew2=pd.concat([df15LV,df15F,df15PC], axis=1)
#if necessary convert to float
dfnew2 = dfnew2.astype(float)
print (dfnew2)
     0    1    2
0  7.8  7.2  7.6
1  7.8  7.1  8.1
2  NaN  7.9  9.9
3  NaN  9.5  NaN

这篇关于如何将系列分为两列 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆