I have this DataFrame:
df=pd.DataFrame.from_items([('id', [14, 49, 21]),
('parameter', [12, 23, 11])])
And I have this list:
[8, 1, 3]
<class 'list'>
I want to append the list values to each value of the column id of the dataframe df, I want something like this:
id parameter
0 148 12
1 491 23
2 213 11
How can I do it?
You can try like so:
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
Output:
id parameter
0 148 12
1 491 23
2 213 11
Using zip
works:
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)]
and the resulting df
is:
>>> df
id parameter
0 148 12
1 491 23
2 213 11
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.