pandas与numpy操作
pandas操作
pandas索引

import pandas as pd
d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
'three': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
'four': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df['one']) #按列label取列
dataframe.iloc 按索引号操作**
print(df.iloc[:,0])#按列下标index取列
print(df.iloc[0]) #按行下标index取行
.loc 按label 操作
print(df.loc['a']) #按行label取行
print(df.loc[:,'one'])
列操作
dataframe.label_of_culomn
dataframe.label_of_culomn.labei_of_row
存文件
各列为一维数组或列表
Label = np.array(Label)
ImageId = list(range(1,len(Label)+1))
#转为pd.DataFrame
df = pd.DataFrame([ImageId,Label],index=['ImageId','Label'])
#转置
df2 = pd.DataFrame(df.values.T,index = df.columns,columns=df.index)
#存文件
with open('final.csv','w',newline='') as f:
df2.to_csv(f)
numpy操作
文件读取
numpy读取csv:
p = r'file_path.csv'
with open(p, encoding='utf-8') as f:
#返回一个二维np数组
data = np.loadtxt(f, delimiter=",", skiprows=1) #以逗号分隔,跳过第一行
#对于单列会返回一维数组,增加维度
data = data[:,np.newaxis]
按行或列取最大值
data = np.argmax(data,axis=1)
#会将
[
[]
··· 转为 [···]
[]
]

浙公网安备 33010602011771号