Python 如何将系列拆分为两列

Python 如何将系列拆分为两列,python,pandas,series,Python,Pandas,Series,我有三个充满数据的列表,我想将它们连接起来以创建一个数据帧 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

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

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

我将其转换为系列并将其浓缩:

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)
这里的cons有一个问题,在每一列中,它都会考虑旧列的名称及其索引的值

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
所以我测试了拆分函数:

dfnew2['a'] = dfnew2[2].split(' ')
但它不起作用,当我尝试拆分这些列时,会发生以下情况:

AttributeError: 'Series' object has no attribute 'split'
是否可以只为每列指定值:

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

我认为您需要使用拆分
apply
,并使用
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
另一种解决方案是使用
列表理解
进行拆分:

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

如果列表长度相等,则只需创建一个空数据框并填充它:

data_activationsLV = [7.8,7.8,7.9,8.2]
data_activationsF = [7.2,7.1,7.9,9.5]
# create an empty dataframe
columns = ['LV', 'F']
index = np.arange(len(data_activationsLV)) # array of numbers for the number of rows
df = pd.DataFrame(columns=columns, index = index)
df['LV'] = data_activationsLV
df['F'] = data_activationsF
df

错误:AttributeError:“Series”对象没有属性“set\u index”对不起,您能将
打印(数据\u activationsLV[:10])
添加到问题中吗?我添加了不同长度的测试数据,对我来说,它工作得很好。你能检查一下吗?对于这一行:dfnew2=dfnew2.apply(lambda x:x.str.split().str[1])我又遇到了一个错误:AttributeError:(“只能使用带字符串值的.str访问器,它在pandas中使用np.object dtype”,u出现在索引0处)以及第二个没有拆分的情况,我得到了这个错误:ValueError:用序列设置数组元素。似乎您正在使用Python3.3(我在2.7中…)可能需要
dfnew2=dfnew2.apply(lambda x:x.astype(str.str.split().str[1])
列表的长度不同