Pandas DataFrame 两列相除生成新列
可以使用 df['new_column'] = df['column1'] / df['column2'] 来将两列相除生成新的一列。
示例代码如下:
import pandas as pd
# 创建 DataFrame
data = {'column1': [1, 2, 3, 4, 5],
'column2': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
# 相除生成新的一列
df['new_column'] = df['column1'] / df['column2']
print(df)
输出结果:
column1 column2 new_column
0 1 2 0.5
1 2 4 0.5
2 3 6 0.5
3 4 8 0.5
4 5 10 0.5
原文地址: https://www.cveoy.top/t/topic/o1AU 著作权归作者所有。请勿转载和采集!