pandas
https://blog.csdn.net/lwgkzl/article/details/80948548
去掉字符串中的空格,转为10进制
test_data.applymap((lambda x: int("".join(x.split()), 2) if type(x) is str else x))
pandas 过滤
条件过滤
读取时间
data = pd.read_csv("covid.csv")
df = data.loc[data["countriesAndTerritories"] == "Afghanistan", ["dateRep", "cases"]]
print(df)
df['dateRep'] = pd.to_datetime(df['dateRep'],format="%d/%m/%Y").apply(lambda x: x.strftime("%Y-%m-%d"))
newdf1 = data[(data.continentExp == "Asia") & (data.countriesAndTerritories == "China")]
newdf2 = data.query('continentExp == "Asia" & countriesAndTerritories == "China"')
newdf3 = data.loc[(data.continentExp == "Asia") & (data.countriesAndTerritories == "China")]
对于名称中带有空格的列,可以使用反引号引起来
data.query("`machine num` == 1")
增加一行
df.loc['new_raw'] = '3'
df
Out[84]:
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
new_raw 3 3 3 3
增加一列
df['new_colu']='12'#向 DataFrame 添加一列,该列为同一值
df
Out[93]:
one two three four new_colu
a 0 1 2 3 12
b 4 5 6 7 12
c 8 9 10 11 12
d 12 13 14 15 12
new_raw 3 3 3 3 12
pandas按行按列遍历Dataframe的几种方式
- iterrows(): 按行遍历,将DataFrame的每一行迭代为(index, Series)对,可以通过row[name]对元素进行访问。
- itertuples(): 按行遍历,将DataFrame的每一行迭代为元祖,可以通过row[name]对元素进行访问,比iterrows()效率高。
- iteritems():按列遍历,将DataFrame的每一列迭代为(列名, Series)对,可以通过row[index]对元素进行访问。
for index, row in df.iterrows():
print(index) # 输出每行的索引
dataframe取多列
tpot_data[[18, 19]]
重新排序
m = np.concatenate((testing_target.values.reshape(-1, 1), y_pred.reshape(-1, 1)), axis=1)
code_origin = pd.DataFrame(m, index=None, columns=["true", "predict"])
code_origin.sort_values("true", inplace=True)

浙公网安备 33010602011771号