简体   繁体   English

如何根据条件替换 Panda 数据框列中的单元格

[英]How do I replace cells in a Panda dataframe column based on a condition

This post consists of two questions, touching on issues I have encountered when trying to replace elements in a Panda dataframe based on a given condition.这篇文章包含两个问题,涉及我在尝试根据给定条件替换 Panda 数据框中的元素时遇到的问题。 I am new with Pandas, so any suggestions will be most helpful.我是 Pandas 的新手,所以任何建议都将是最有帮助的。

1: Modifying strings of a certain length 1:修改一定长度的字符串

Consider column A in a Panda dataframe object, df:考虑 Panda 数据框对象 df 中的 A 列:

SSIC
103
1040
1054
1065
107

I want to append the integer 0 to each cell that is of length less than four.我想将整数 0 附加到长度小于 4 的每个单元格。 That is, I want to obtain:也就是说,我想获得:

SSIC
0103
1040
1054
1065
0107

The values are of type float64.这些值属于 float64 类型。

Currently, I have used this method:目前,我使用了这种方法:

SSIC1 = df['SSIC'].astype('int64').astype(str)

for i,n in enumerate(SSIC1):
    if len(SSIC1[i]) == 4:
       SSIC1[i] = '0' + SSIC1[i]

df['SSIC'] = SSIC1

It works, but I wonder if it is rather long-winded.它有效,但我想知道它是否相当冗长。 Is there a more direct way to resolve this?有没有更直接的方法来解决这个问题?

2: Setting with Copy Warning when using where clause 2:使用 where 子句时设置复制警告

I have the following two columns in a dataframe我在数据框中有以下两列

A  B
2  1
3  4
4  6
5  4
5  2

I want to replace cells in column A that take on the value 5, with the values on the same row in B.我想用 B 中同一行上的值替换 A 列中值为 5 的单元格。

I have used the where condition:我使用了 where 条件:

df['A']=df['A'].where(df['A'] == 5, df['B'], inplace=True)

​But it gives me the following error:但它给了我以下错误:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. SettingWithCopyWarning:试图在 DataFrame 中切片的副本上设置值。 Try using .loc[row_indexer,col_indexer] = value instead尝试使用 .loc[row_indexer,col_indexer] = value 代替

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy if name == ' main ':请参阅文档中的注意事项: http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy if name == ' main ':

This warning did not occur the first time I ran it.我第一次运行它时没有出现这个警告。 Any ideas why it has popped up?任何想法为什么它突然出现?

For your first part call the vectorised str.zfill :对于您的第一部分,请调用矢量化str.zfill

In [167]:
df['SSIC'].astype(str).str.zfill(4)

Out[167]:
0    0103
1    1040
2    1054
3    1065
4    0107
Name: SSIC, dtype: object

You may not need the call astype if the dtype is already str你可能不需要调用astype如果dtype已经是str

Use the dataframe.at[] function.使用 dataframe.at[] 函数。 That is:那是:

df.at[row_index,column_name] = 'the desired assignment' df.at[row_index,column_name] = '所需的分配'

for example:例如:

df = pd.DataFrame(data=[['iqbal',2,88]],columns=['name','roll','marks'],index=[0])
df.at[0,'name']= 'Joy'

` `

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

相关问题 如何根据条件替换 pandas DataFrame 中的值? - How do I replace a value in pandas DataFrame based on a condition? 我如何过滤熊猫数据框并根据其他列和其他条件仅保留行 - How do i filter panda dataframe and keep only row based on other column and other conditions 如何根据Python中的简单条件过滤熊猫数据框? - How do I filter a Panda dataframe based on a simple criteria in Python? 如何基于列合并熊猫数据框? - How to merge panda dataframe based on column? 如何在python panda数据框中找到所有零单元格并替换它们? - How to find all the zero cells in a python panda dataframe and replace them? 如何根据第二个 Dataframe 值的条件替换 Dataframe 列值 - How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values 如何在条件下替换数据框的值 - How do i replace the values of a dataframe on a condition 如何根据公共列将 dataframe 的列值替换为另一个 dataframe 的值? - How do I replace column values of a dataframe with values of another dataframe based on a common column? 如何根据具有一系列值的条件替换 pd 数据框列中的值? - How to Replace values in a pd dataframe column based on a condition with a range of values? 如何根据条件用列名替换熊猫数据框中的值? - How to replace a value in a pandas dataframe with column name based on a condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM