[英]add array into pandas dataframe
I have a pandas dataframe called A with one column called "a": 我有一个名为A的pandas数据框,其中一列名为“a”:
I have one array that looks like: [4,3,2]. 我有一个看起来像的数组:[4,3,2]。 I want to insert this array into the dataframe and give the new column the name b. 我想将此数组插入到数据框中,并为新列指定名称b。 How do I do that? 我怎么做?
Expected output: 预期产量:
As @mgc pointed out in the comment you could do df['b'] = l
: 正如@mgc在评论中指出的那样你可以做df['b'] = l
:
import pandas as pd
from io import StringIO
data="""
date a
2016-01-19 3
2016-01-20 1
2016-01-21 2
"""
df = pd.read_csv(StringIO(data), sep='\s+')
df = df.set_index('date')
df.index = pd.to_datetime(df.index)
print(df)
a
date
2016-01-19 3
2016-01-20 1
2016-01-21 2
l = [4,3,2]
df['b'] = l
print(df)
a b
date
2016-01-19 3 4
2016-01-20 1 3
2016-01-21 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.