相关文章推荐
打篮球的汽水  ·  Spring Boot测试 - 简书·  2 年前    · 
def DataframeMinTest(): df = pd.DataFrame(np.random.randn(1000, 3), columns=list('ABC')) print(df.shape) # 方式一 取AB两列小值合成D列 df['D'] = df.loc[:, ['A', 'B']].min(axis=1) # 方式二 取AC两列大值合成E列 df['E'] = np.max(df.loc[:, ['A', 'C']].values, axis=1) print(df.head(10)) if __name__ == '__main__': DataframeMinTest()

(1000, 3)
A               B               C               D             E
0 -0.879277 -1.027903 -0.602419 -1.027903 -0.602419
1  1.010298  1.604261 -0.097757  1.010298  1.010298
2 -0.228731  0.646538  0.020998 -0.228731  0.020998
3  1.404384 -1.681164  0.134697 -1.681164  1.404384
4 -0.368503  0.270716  1.419268 -0.368503  1.419268
5  2.238605  1.628516 -1.984287  1.628516  2.238605
6  0.652273 -0.166380  1.855101 -0.166380  1.855101
7  0.602067  0.604722 -0.640275  0.602067  0.602067
8 -2.775687 -2.649085  0.502292 -2.775687  0.502292
9  0.031345  0.419377 -0.445941  0.031345  0.031345

s = pd.Series([10.0247,10.0470, 10.0647,10.0761,15.0800,10.0761,10.0647,10.0470,10.0247,10.0,9.9753,9.9530,9.9353,9.9239,18.92,9.9239,9.9353,9.9530,9.9753,10.0]) df = pd. DataFrame (s) df.columns=['value'] df['val_1'] = df['val 3、根据现有列计算生成新的列 dataframe 根据某列的 生成新的列 df2[‘是否逾期’]=df2.apply(lambda x:0 if x.应付日期>today_time else 1,axis=1) df2[‘是否到期90天’]=(today_time - 可以使用 df.iloc[:, :-1] 来获取除了df的最后一列之外的所有列,然后将其赋 给新的变量df2即可创建df的副本df2。 import pandas as pd # 创建一个示例数据 df = pd. DataFrame ({'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}) # 创建df的副本df2 df2 =... 文章目录一、整体介绍1.1 Pandas 优点1.2 Pandas 的数据类型1.3 Series 和 DataFrame 共性索引和列信息查看标量化计算查看是否包含某个元素 二、Series和 DataFrame 常用操作2.1 SeriesSeries的索引操作2.2 DataFrame 2.2.1 创建 DataFrame 对象输入数据类型实例(创建及reindex)2.2.2 查询和赋 通过行列索引查询条件查询数据赋 2.2.3 索引和切片(待补充)索引对象切片map和apply函数(待补充)元素级运算(待补充 接上节Jingwei:从零开始使用 python / pandas 操作Excel (1)​zhuanlan.zhihu.com pandas 读取Excel数据表进阶接下来我们再来看一个例子。其中B2至D2为列标题。图1 Excel数据表示例注意,此时的数据表不再是Excel文件的第一张表。用默认方式读取Excel文件将获得Sheet1的数据。然而Sheet1没有数据,所以读取的结果就是一个空数据表。图2 ... df_new.eval('Primary_Total_edu = Primary_Total_enrollment - Primary_Total_drop', inplace=True) df_new.eval('Upper_Primary_Total_edu = Upper_Primary_Total_enrollment - Upper_Primary_Total_drop', inplace=True) print(df_new.head()) eval非常方便, 上图将 小学(初中)入学率这一列 代码如下:data=pd. DataFrame (np.random.randn(5,5),columns=list('abcde'))def fun1(s):    if s >0.9:        return 1    elif s>0.5:        return 0    else:        return -1def fun2(s,d):    if np.exp(s...