大家好,我是Pandas的新手
我有多个CSV文件,如下所示:
john_age.csv
john_gender.csv
john_weight.csv
mike_age.csv
mike_gender.csv
mike_weight.csv
smith_age.csv
smith_gender.csv
smith_weight.csv
...
...
每个csv文件都有一个简单的字符串或数字,如下所示:
john_age.csv 54
john_gender.csv male
john_weight.csv 65.4
基本上,我想让整个dataframe看起来像这样:
age gender weight
john 54 male 65.4
mike 23 male 86.5
smith 52 female 54
我怎样才能做到这一点?
我认为关键的想法是将每个csv文件名合并到dataframe中,但到目前为止,我只能使用glob.glob和append函数读取多个csv文件,但append函数不是解决方案:
csv_path = \mypath\
filenames = glob.glob(csv_path + '\*.csv')
dfs= []
for file in filenames:
dfs.append(pd.read_csv(file))
提前感谢!!
4条答案
按热度按时间r1wp621o1#
这将从文件中创建一个框架。
2skhul332#
我说的就是这种事
0vvn1miw3#
您可以使用
pd.concat()
在一行中完成此操作xtupzzrd4#