GroupID.csv是文件名.有这样的多个文件,但值范围在同一XML文件中定义.我正在尝试将它们分组
我怎样才能做到这一点?
UPDATE1:
根据BobHaffner的评论,我已经做到了这一点
import pandas as pd
import glob path =r'path/to/files'
allFiles = glob.glob(path + "/*.csv")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
df = pd.read_csv(file_,index_col=None, header=None)
df['file'] = os.path.basename('path/to/files/'+file_)
list_.append(df)
frame = pd.concat(list_)
print frame
得到这样的东西:
我需要根据XML文件中的bin对值进行分组.我真的很感激任何帮助.
解决方法:
为了打开你的系列,你应该使用the pd.cut()
function,如下所示:
df['bin'] = pd.cut(df['1'], [0, 50, 100,200])
0 1 file bin
0 person1 24 age.csv (0, 50]
1 person2 17 age.csv (0, 50]
2 person3 98 age.csv (50, 100]
3 person4 6 age.csv (0, 50]
4 person2 166 Height.csv (100, 200]
5 person3 125 Height.csv (100, 200]
6 person5 172 Height.csv (100, 200]
如果您想自己命名垃圾箱,可以使用labels =参数,如下所示:
df['bin'] = pd.cut(df['1'], [0, 50, 100,200], labels=['0-50', '50-100', '100-200'])
0 1 file bin
0 person1 24 age.csv 0-50
1 person2 17 age.csv 0-50
2 person3 98 age.csv 50-100
3 person4 6 age.csv 0-50
4 person2 166 Height.csv 100-200
5 person3 125 Height.csv 100-200
6 person5 172 Height.csv 100-200
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。