[英]Find rows based on number of digits
I'm working on a dataframe that has 2000 rows but for this purpose I have created this simple data frame in which I want to find all rows containing 3 or less digits in the col2 column.我正在处理一个有 2000 行的数据框,但为此我创建了这个简单的数据框,我想在其中找到 col2 列中包含 3 个或更少数字的所有行。 Here is the dataframe:
这是数据框:
d = {'col1': [10000, 2000,300,4000,50000], 'col2': [10, 20000, 300, 4000, 100]}
df = pd.DataFrame(data=d)
col1 col2
0 10000 10
1 2000 20000
2 300 300
3 4000 4000
4 50000 100
Area int64
Price int64
dtype: object
After that I would like to create a new column col3 where the values from col2 column from those filtered rows (with 3 or less digits) will be multiplied by their values from the col1 column while the other rows stays the same.之后,我想创建一个新列 col3,其中来自那些过滤行(具有 3 个或更少数字)的 col2 列的值将乘以它们来自 col1 列的值,而其他行保持不变。
Here's the expected output:这是预期的输出:
col1 col2 col3
0 10000 10 100000
1 2000 20000 20000
2 300 300 90000
3 4000 4000 4000
4 5000 100 500000
col1 int64
col2 int64
col3 int64
dtype: object
Thanks in advance!提前致谢!
use np.where to create a condition, since these are number, we can check that column2 value is less than 1000使用 np.where 创建条件,因为这些是数字,我们可以检查 column2 的值是否小于 1000
cond = (df['col2'] < 1000)
choice = (df['col1'] * df['col2'])
df['col3'] = np.where(cond, choice, df['col2'])
df
col1 col2 col3
0 10000 10 100000
1 2000 20000 20000
2 300 300 90000
3 4000 4000 4000
4 50000 500 25000000
You can try Series.mask
你可以试试
Series.mask
df['col4'] = df['col2'].mask(df['col2'] < 1000, df['col2'] * df['col1'])
print(df)
col1 col2 col3 col4
0 10000 10 100000 100000
1 2000 20000 20000 20000
2 300 300 90000 90000
3 4000 4000 4000 4000
4 5000 100 500000 500000
Vanilla pandas code using df.apply
使用
df.apply
香草熊猫代码
def custom_fill(cols):
if cols[1] < 1000:
return cols[0] * cols[1]
else:
return cols[1]
df['col3'] = df[['col1','col2']].apply(custom_fill, axis=1)
Output:输出:
col1 col2 col3
0 10000 10 100000
1 2000 20000 20000
2 300 300 90000
3 4000 4000 4000
4 50000 100 5000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.