[英]find min and max of each column in pandas without min and max of index
[英]To find datatypes of column in a file given, to find max and min value of each column, in case of string find max, min string based on length
給定文件的數據:
Name age weight
John 21 78.5
kennedy 39 68.3
預期 output:
col_name dtype
Name str max: kennedy min: john
age int max: 39 min: 21
weight float max: 78.5 min: 68.3
****誰能幫我解決?**
我也試過這個,但不知道如何找到它的最大值,字符串的最小值,我只是為 int,float 做了。**
import pandas as pd
df=pd.read_csv(P1-UK-Bank-Customers.csv")
for col in df.select_dtypes([np.int8, np.int16, np.int32, np.int64, np.float]):
print('column: ', col)
print('max: ',df[col].max())
print('min: ',df[col].min())
print()**
嘗試這樣的事情:
def min_mx_dtype(x):
return pd.Series(index=['min', 'max', 'dtype'],data=[x.min(), x.max(), x.dtype])
print(df.apply(min_mx_dtype).T.reset_index())
index min max dtype
0 Name John kennedy object
1 age 21 39 int64
2 weight 68.3 78.5 float64
您可以從字典列表中創建 dataframe。 然后以您想要的任何格式打印出來。 對於字符串,min 和 max 將等效於按升序排序的字符串列表中的第一個和最后一個值。
vals = []
for col in df.columns:
vals.append({'col_name': col,
'dtype': df[col].dtype,
'max': df[col].max(),
'min': df[col].min()})
df = pd.DataFrame(vals)
Output
col_name dtype max min
0 Name object kennedy John
1 age int64 39 21
2 weight float64 78.5 68.3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.