[英]Inserting a Pandas Series into a DataFrame makes all values Nan
As the title implies I have converted two dictionaries into series like so and I tried to insert them into the data frame df. 就像标题所暗示的那样,我已经将两个词典转换成系列,就像这样,我试图将它们插入数据框df中。
first_series = pd.Series(first_dict, name='State Names')
second_series = pd.Series(second_dict, name='City Names')
column_loc=list(df.columns.values).index("ipAddr")
df.insert(column_loc+1, 'State Names', first_series)
df.insert(column_loc+2, 'City Names', second_series)
When I run this however I get 当我运行这个但是我得到
ipAddr State Names City Names ...
respID ...
10018 ***.**.**.** NaN NaN ...
10025 **.**.**.** NaN NaN ...
the series are as follows 系列如下
10018 Bedford
10025 Vancouver
...
10267 Lompoc
10280 Pikesville
Name: State Names, dtype: object
--------------------------------------------------------
10018 Ohio
10025 Washington
...
10267 California
10280 Maryland
Name: City Names, dtype: object
I've checked that both the dictionaries and the resulting series are populated so I don't understand why this is occurring. 我检查了字典和结果系列是否都已填充,因此我不明白为什么会这样。
Thank you. 谢谢。
edit: A similar question got asked here but wasn't answered When I insert pandas Series into dataframe, all values become NaN 编辑:在这里问了一个类似的问题,但没有得到回答当我将pandas Series插入数据框时,所有值都变为NaN
It turned out the Series has less rows than the df. 事实证明,该系列的行少于df。 Also, Series.values need to be used while assigning it to a df column.
同样,在将Series.values分配给df列时也需要使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.