如何将转换后的变量与 Python 中的其他变量一起添加到 Dataframe 中?

时间:2021-08-02 01:04:21

标签: python pandas dataframe jupyter-notebook data-analysis

我是熊猫的新手,有时事情会让我感到困惑。所以我在 Dataframe(df) 列名称中有一个列是“黑人或非洲裔美国人”,我将其从字符串百分比转换为浮点数,但现在我想将转换后的变量添加回 Dataframe 或创建一个包含这些变量的新 Dataframe从以前开始。我该怎么做?请帮忙这是错误消息':1:SettingWithCopyWarning:

正在尝试在来自 DataFrame 的切片副本上设置值。 尝试使用 .loc[row_indexer,col_indexer] = value 代替'

black = race_df['Black or African American'].str.rstrip('%').astype(float) / 100
black.head(5)   


0    0.264
1    0.031
2    0.041
3    0.153
4    0.055
Name: Black or African American, dtype: float64

race_df['Black or African American'] = race_df['Black or African  American'].str.rstrip('%').astype(float) / 100

我已经尝试了上面的代码,但仍然出现错误

0 个答案:

没有答案