简体   繁体   English

pandas 在添加新列时返回一个系列而不是 dataframe

[英]pandas returns a series instead of a dataframe while adding a new column

I am trying to perform a simple operation of creating a new dataframe from an existing dataframe and also trying to add a new column at the same time.我正在尝试执行一个简单的操作,即从现有的 dataframe 创建一个新的 dataframe 并同时尝试添加一个新列。 However, pandas returns a series instead of a dataframe.但是,pandas 返回一个系列而不是 dataframe。

controls['DESC']=cells1['CEL_DESCRIPTION']

I get the following error for: print(controls.info())我收到以下错误: print(controls.info())

AttributeError: 'Series' object has no attribute 'info'

I use this code and don't get an error:我使用此代码并且没有收到错误:

import pandas as pd

old = pd.DataFrame({'temp_c': [17.0, 25.0]},index=['Portland', 'Berkeley'])
print(old)

new = old.copy().assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
print();print(new)

print();print(new.info())

output: output:

          temp_c
Portland    17.0
Berkeley    25.0


          temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0


<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Portland to Berkeley
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   temp_c  2 non-null      float64
 1   temp_f  2 non-null      float64
dtypes: float64(2)
memory usage: 48.0+ bytes
None

EDITED已编辑

On edit version I edit code for adding columns name with space like temp c instead of temp_c and temp f instead of temp_f like below:在编辑版本中,我编辑代码以添加带有空格的列名称,例如temp c而不是temp_ctemp f而不是temp_f ,如下所示:

import pandas as pd

old = pd.DataFrame({'temp c': [17.0, 25.0]},index=['Portland', 'Berkeley'])
print(old)

new = old.copy().assign(**{'temp f': (lambda x: x['temp c'] * 9 / 5 + 32)})

print();print(new)

print();print(new.info())

output: output:

          temp c
Portland    17.0
Berkeley    25.0

          temp c  temp f
Portland    17.0    62.6
Berkeley    25.0    77.0

<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, Portland to Berkeley
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   temp c  2 non-null      float64
 1   temp f  2 non-null      float64
dtypes: float64(2)
memory usage: 48.0+ bytes
None

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

相关问题 pandas中的新列 - 通过应用列表groupby将数组添加到数据框中 - New column in pandas - adding series to dataframe by applying a list groupby 将系列作为新列添加到 pandas dataframe 时缺少行 - Missing rows when adding a series as a new column to a pandas dataframe Pandas Apply 返回系列而不是 dataframe - Pandas Apply returns a Series instead of a dataframe 向Pandas数据帧添加系列会产生NaN列 - Adding series to Pandas dataframe yields column of NaN 添加列使用 pd.Series 将 NaN 返回到 Pandas DataFrame 进行第一次迭代而不是字符串 - Add column using pd.Series returns NaN to Pandas DataFrame for the first iteration instead of string 将新的dataFrame列添加到pandas中的相同数据框 - Adding new dataFrame column to the same dataframe in pandas 将Pandas Dataframe-column添加到新数据帧 - Adding a Pandas Dataframe-column to a new dataframe 将 pandas 系列值添加到 pandas Z6A8064B5DF479455507DZ553 - add pandas series values to new dataframe column at end of pandas dataframe Pandas Dataframe和Series联接返回空的Dataframe或NaN列 - Pandas Dataframe and Series join returns empty Dataframe or NaN column 在 Pandas DataFrame 中添加新列时结果不一致。 它是一个系列还是一个值? - Inconsistent results when adding a new column in Pandas DataFrame. Is it a Series or a Value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM