简体   繁体   English

将Pandas Dataframe-column添加到新数据帧

[英]Adding a Pandas Dataframe-column to a new dataframe

Using Pandas, I have some data that I want to add to my ``results'' dataframe. 使用Pandas,我有一些数据要添加到我的``results''数据帧中。 That is, I have 就是,我有

naics = someData naics = someData

Which can look like this 哪个看起来像这样

   indnaics  ind1990
89    81393      873

however, it can have more than one row. 但是,它可以有多行。 I want to add these to my results dataframe, together with a variable called year. 我想将这些添加到我的results数据框中,以及一个名为year的变量。 In case there is more than one row, it should be the same year value for all rows. 如果有多行,则应该是所有行的相同year值。 This is what I am trying so far 这是我到目前为止所尝试的

for job in jobs:
    df2 =  iGetThisFromJob()
    years = df2.year.unique()
    naics = iGetThisFromJob()
    if len(naics) == 0:
        continue

    for year in years:
        wages = df2.incwage[df2.year == year]
    # Add all the data to results, this is how I try it
        rows = pd.DataFrame([dict(year=year, incwage=mean(wages), )])
    # I also want to add the column indnaics from my naics 
        rows['naics'] = naics.indnaics
        results = results.append(rows, ignore_index=True)

However, despite naics.indnaics being full, I cannot add it this way to the rows object. 但是,尽管naics.indnaics已满,但我无法以这种方式将其添加到rows对象中。

naics.indnaics naics.indnaics

Out[1052]: 
89    81393

rows['naics'] = naics.indnaics rows rows ['naics'] = naics.indnaics行

Out[1051]: 
        incwage  year naics
0  45853.061224  2002   NaN

If there is anything else that is not nice with my code, please tell. 如果还有其他任何与我的代码不相符的内容,请告诉我们。 I'm only beginning to learn pandas. 我才开始学习熊猫。

Thanks! 谢谢!

/edit Expected output: /编辑预期输出:

        incwage  year   naics
0  45853.061224  2002   81393
0  45853.061224  2002   12312

/edit Suggested solution: / edit建议的解决方案:

index = arange(0, len(naics))
columns = ['year', 'incwage', 'naics']
rows = pd.DataFrame(index=index, columns=columns)
rows.year = year
rows.incwage = mean(wages)
rows.naics = naics.indnaics.values

The reason you get a NaN value, is because the index does not match (in rows['naics'] = naics.indnaics rows has index 0, while naics.indnaics has index 89), and assigning the value will try to align the indices. 你得到一个NaN值的原因是因为索引不匹配( rows['naics'] = naics.indnaics rows有索引0,而naics.indnaics有索引89),并且赋值该将尝试对齐指数。

You could for example solve that by taking only the value (by eg naics.indnaics.values ). 例如,你可以通过仅取值(例如naics.indnaics.values )来解决这个问题。 With a toy example: 以玩具为例:

In [30]: df = pd.DataFrame({'A':[0], 'B':[1]})
In [31]: df
Out[31]: 
   A  B
0  0  1


In [32]: s = pd.Series([2], index=[83])
In [33]: s
Out[33]: 
83    2
dtype: int64

In [35]: df['new_column'] = s
In [36]: df
Out[36]: 
   A  B  new_column
0  0  1         NaN

In [37]: df['new_column'] = s.values
In [38]: df
Out[38]: 
   A  B  new_column
0  0  1           2

If you want to add the series with possibly more values, there are a couple of options. 如果要添加可能更多值的系列,可以使用几个选项。 I think of: 我想:

Eg reindexing the dataframe first to the length of the series: 例如,首先将数据帧重新索引到系列的长度:

In [75]: s
Out[75]: 
83    2
84    4
dtype: int64

In [76]: df
Out[76]: 
   A  B
0  0  1

In [77]: df = df.reindex(np.zeros(len(s)))
In [78]: df
Out[78]: 
   A  B
0  0  1
0  0  1

In [79]: df['new_column'] = s.values

In [80]: df
Out[80]: 
   A  B  new_column
0  0  1           2
0  0  1           4

or the other way around, add the dataframe to the series (that you first convert to a dataframe): 或者反过来,将数据框添加到系列(您首先转换为数据框):

In [90]: ss = s.to_frame().set_index(np.array([0,0]))
In [91]: ss[df.columns] = df
In [92]: ss
Out[92]: 
   0  A  B
0  2  0  1
0  4  0  1

[2 rows x 3 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM