#生成DataFrame,保存至文件 from pandas import DataFrame import numpy as np import pandas as pd t={ "称呼": ["爸爸","妈妈","宝宝"], "年龄": [18, 30, np.nan], "性别": [None, "male", "female"], "生日": ["2000-02-10", "1988-10-17", None] } df =DataFrame(t) df.to_csv("Result.csv",header=1,index=0) #保存列名、不保存行索引
#读取文件,生成DataFrame import pandas as pd df = pd.read_csv("Result.csv") #扩展名也可不是 .csv print(df) print(df.isnull())
#显示“年龄”列,行号1,2的单元格,求和 import pandas as pd df = pd.read_csv("Result.csv") print(df.loc[[0,1],['年龄']]) print(type(df.loc[[0,1],['年龄']])) print(df.loc[[0,1],['年龄']].sum())
#将DataFrame中的空值设为0 import pandas as pd df = pd.read_csv("Result.csv") df.fillna(0,inplace = True) print(df)
#插入列 import pandas as pd df = pd.read_csv("Result.csv") df.insert(loc=2, column='插入列', value="插队") print(df)
或: df.loc[:,新列名]=值
#插入行(实际是两个表在纵轴方向上拼接)并去除重复数据行 import pandas as pd df = pd.read_csv("Result.csv") df1=DataFrame({ "插入行": ["插入1","插入1","插入1"] }) pd.concat([df1,df2],ignore_index = True) #当两个表中有重叠的数据时,使用drop_duplicates()去除重复数据 pd.concat([df1,df2],ignore_index = True).drop_duplicates() print(df)
#根据现有列生成新的列 import pandas as pd from pandas import DataFrame def getlevel(年龄): if 年龄 > 25: return "老" elif 年龄 > 15: return "中" else: return "青" df = pd.read_csv("Result.csv") # df['level'] = df.apply(lambda x: getlevel(x['年龄']), axis=1) df['level'] = df.apply(lambda x: getlevel(x.年龄), axis=1) print(df)
#删除列 df.drop(axis = 0, index=[0], inplace=True)
#列改名,a,b变为A,B df.rename(columns={"A": "a", "B": "c"},inplace=True)
修改单元格,记忆公式:
单值修改:df.loc[行索引,列索引]
单行修改:df.loc[行索引]或df.iloc[行索引];
单列修改:df[列索引]、df.loc[:,列索引]或df.iloc[:,列索引]
按条件修改: df.loc[[筛选后的DataFrame].index,列索引]
mask(条件判断,条件成立的值)
where(条件判断,条件不成立的值)
#修改单元格:在指定列中找符合条件的单元格,对于这些单元格所在行,修改与指定列相交的单元格的值 import pandas as pd from pandas import DataFrame df = pd.read_csv("Result.csv") df.loc[df["称呼"]=='爸爸',['称呼','年龄']]='father' print(df)
#使用正则修改替换1 import pandas as pd from pandas import DataFrame df = pd.read_csv("Result.csv") df['称呼']=df['称呼'].str.replace( pat='^(爸+)$', repl=lambda x: '0'*(4-len(x[0]))+x[0]+'ok', regex=True ) print(df)
#使用正则修改替换2 import pandas as pd df = pd.read_csv("Result.csv") df.replace( '^(爸+)$', r'\1神了', regex=True, inplace=True ) print(df)
#转置 df=df.T
#移动整行 import pandas as pd from pandas import DataFrame df = pd.read_csv("Result.csv") df.iloc[0, :] = df.iloc[0, :].shift(-1)
#移动部分单元格 import pandas as pd from pandas import DataFrame df = pd.read_csv("Result.csv") print(df) df.iloc[0, 1:2] = df.iloc[0, 1:2].shift(-1) print(df)
参考:https://www.cnblogs.com/testzcy/p/12077820.html
https://blog.csdn.net/m0_62011685/article/details/124827501
https://blog.csdn.net/m0_62011685/article/details/124716966
https://blog.csdn.net/weixin_42322206/article/details/124226550
https://www.cjavapy.com/article/774/#google_vignette
标签:常用,示例,df,pd,print,import,csv,pandas From: https://www.cnblogs.com/pu369/p/17150852.html