读取文件
读取excel文件
1 2
| import pandas as pd df = pd.read_excel(file_path, sheet_name)
|
读取csv文件
1 2
| import pandas as pd df = pd.read_csv(file_path)
|
写入文件
创建一个DataFrame对象,设置列名,data可为二维数组
1
| df = pd.DataFrame(data=None, columns = ["disease", "relation", "object"])
|
写入文件
1
| df.to_csv(file_path, index = False, sep=",")
|
循环迭代数据
1 2
| for row in range(df.shape[0]): data = df.loc[row, "Month_No"]
|
插入整列数据
1 2 3 4 5
| import pandas as pd s = pd.Series([6,8,3,1,12]) df = pd.DataFrame(s,columns=['Month_No']) df.insert(1,"No_of_days",[30,31,31,31,31],True) df
|
|
Month_No |
No_of_days |
0 |
6 |
30 |
1 |
8 |
31 |
2 |
3 |
31 |
3 |
1 |
31 |
4 |
12 |
31 |
尾部插入数据
append插入
1 2
| df = df.append({"Month_No":7,"No_of_days":31},ignore_index=True) df
|
concat插入
append方法已经废弃,推荐使用concat方法
1 2 3
| df_tmp=pd.DataFrame([[7,31],[4,30]], columns=["Month_No", "No_of_days"]) df = pd.concat([df, df_tmp], ignore_index = True) df
|
|
Month_No |
No_of_days |
0 |
6 |
30 |
1 |
8 |
31 |
2 |
3 |
31 |
3 |
1 |
31 |
4 |
12 |
31 |
5 |
7 |
31 |
6 |
4 |
30 |
修改列名
暴力修改
1 2
| df.columns=['MonthNo','NoOfDays'] df
|
|
MonthNo |
NoOfDays |
0 |
6 |
30 |
1 |
8 |
31 |
2 |
3 |
31 |
3 |
1 |
31 |
4 |
12 |
31 |
利用rename修改
注意:inplace
参数不能省略
1 2
| df.rename(columns={'MonthNo':'Month_No','NoOfDays':'No_of_days'},inplace=True) df
|
|
Month_No |
No_of_days |
0 |
6 |
30 |
1 |
8 |
31 |
2 |
3 |
31 |
3 |
1 |
31 |
4 |
12 |
31 |
删除列数据
df.drop()
axis=1: 删除列
1
| feature_1 = df_feature_all.drop(labels=["structure of ionizable lipid (SMILE)","IgG"],axis=1).to_numpy()
|
Reference
- pandas尾部追加行记录append