Python 如何在数据框中划分两列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36619631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:08:01  来源:igfitidea点击:

How to divide two column in a dataframe

pythonrpandasdataframe

提问by RHTM

So, in my dataframe I have 2 columns. And I'd like to divide these 2 columns (a & b), value by value, and show it.

所以,在我的数据框中,我有 2 列。我想将这 2 列 (a & b) 按值分开并显示出来。

import pandas as pd

csv1=pd.read_csv('auto
Column A | Column B |
12-------|--2-------|
14-------|--7-------|
16-------|--8-------|
20-------|--5-------|
Result
6
2
2
4
.csv') csv2=pd.read_csv('auto
In [158]:
df['Result'] = df['Column A']/df['Column B']
df

Out[158]:
   Column A  Column B  Result
0        12         2     6.0
1        14         7     2.0
2        16         8     2.0
3        20         5     4.0
.csv') df1 = pd.DataFrame(csv1, columns = ['Column A','Column B']) df2 = pd.DataFrame(csv2, columns = ['Column A','Column B']) dfnew = pd.concat([df1, df2])

The columns:

列:

##代码##

and the expected result

和预期的结果

##代码##

Please help me to find the way.

请帮我找路。

回答by EdChum

Just divide the columns:

只需划分列:

##代码##