python数据框dataframe功能总结(转载)
1.数据框增加一列,df['new'] = test[0]表示给df新增一列,列名为new,整列值为test[0];
-
import pandas as pd
-
-
a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
-
df = pd.DataFrame(a)
-
test = ['qqq','eeee']
-
df['new'] = test[0]
-
print(df)
2.数据框列内容筛选,
df1 = df[df['name'].isin([9,8])]表示筛选name列为9和8的数据框;
df2 = df[~df['name'].isin([9,8])]表示筛选name列不是9和8的数据框,~表示反;
df3 = df[(df['name'] > 9) | (df['name'] < 8)]表示筛选name列大于9或小于8的数据框,|表示或;
df4 = df[(df['ip'] > 2) & (df['ip'] <= 4)]表示筛选ip列大于2且小于等于4的数据框,&表示且。
-
import pandas as pd
-
-
a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
-
df = pd.DataFrame(a)
-
df1 = df[df['name'].isin([9,8])]
-
df2 = df[~df['name'].isin([9,8])]
-
df3 = df[(df['name'] > 9) | (df['name'] < 8)]
-
df4 = df[(df['ip'] > 2) & (df['ip'] <= 4)]
-
print(df1)
-
print(df2)
-
print(df3)
-
print(df4)
3.数据框,获取行数、列数及第几行第几列的值
-
import pandas as pd
-
-
a = {'ip':[1,2,3,4],'name':[0,9,8,77],'time':['qq','ww','ee','rr']}
-
df = pd.DataFrame(a)
-
print(df)
-
colum_size = df.columns.size#列数 2
-
row_size = df.iloc[:,0].size#行数 3
-
num1 = df.iloc[[0]].values[0][0]#第一行第一列的值 1
-
num2 = df.iloc[[1]].values[0][1]#第二行第二列的值 9
-
print(colum_size)
-
print(row_size)
-
print(num1)
-
print(num2)
4.数据框固定列顺序
-
import pandas as pd
-
-
a = {'ip':[1,2,3,4],'name':[0,8,8,77],'time':['qq','ww','ee','rr']}
-
df = pd.DataFrame(a)
-
b = ['name','ip','time']
-
df1 = df.loc[:,b]
-
print(df1)