我有这个DataFrame:
df=pd.DataFrame.from_items([('id', [14, 49, 21]),
('parameter', [12, 23, 11])])
我有这个清单:
[8, 1, 3]
<class 'list'>
我想将列表值附加到dataframe df的列id的每个值,我想要这样的东西:
id parameter
0 148 12
1 491 23
2 213 11
我该怎么做?
答案 0 :(得分:1)
你可以尝试这样:
character encoding
输出:
import pandas as pd
df = pd.DataFrame.from_items([('id', [14, 49, 21]), ('parameter', [12, 23, 11])])
l = iter([8, 1, 3])
df.id = df.id.apply(lambda x: str(x)+str(next(l))).astype(int)
print df
答案 1 :(得分:0)
使用zip
有效:
import pandas as pd
df = pd.DataFrame.from_items([('id', [14, 49, 21]), ('parameter', [12, 23, 11])])
l = [8, 1, 3]
df['id'] = [int(''.join([str(j) for j in i])) for i in zip(df['id'], l)]
,结果df
为:
>>> df
id parameter
0 148 12
1 491 23
2 213 11
答案 2 :(得分:0)
import pandas as pd
df = pd.DataFrame.from_items([('id', [14, 49, 21]), ('parameter', [12, 23, 11])])
lista = [8,1,3]
for i in range(len(lista)):
df['id'][i] = int(str(df['id'][i]) + str(lista[i]))
print df
id parameter
0 148 12
1 491 23
2 213 11