[英]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
data of a given file:给定文件的数据:
Name age weight
John 21 78.5
kennedy 39 68.3
expected output:预期 output:
col_name dtype
Name str max: kennedy min: john
age int max: 39 min: 21
weight float max: 78.5 min: 68.3
****can anyone help me with a solution?** ****谁能帮我解决?**
Also i tried this but don't know how to find it max, min for string, i just did for int, float .**我也试过这个,但不知道如何找到它的最大值,字符串的最小值,我只是为 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()**
Try something like this:尝试这样的事情:
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
You can create a dataframe from a list of dicts.您可以从字典列表中创建 dataframe。 Then print it out in whatever format you want.
然后以您想要的任何格式打印出来。 For strings, min and max would be equivalent to the first and last value in a list of string sorted in ascending order.
对于字符串,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 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.