根据熊猫中的列值导出到CSV

时间:2018-06-26 18:27:32

标签: python python-3.x pandas dataframe

我有这样的df

user = pd.DataFrame({'User':['101','101','101','102','102','101','101','102','102','102'],'Country':['India','Japan','India','Brazil','Japan','UK','Austria','Japan','Singapore','UK'],'Count':[50,1,2,5,6,89,10.9,10,5,6]})

像这样将每个用户数据导出到单独的csv文件中

user_101 = user[user['User'] == '101']
user_102 = user[user['User'] == '102']

user_101.to_csv('user_101.csv',sep=',')
user_102.to_csv('user_102.csv',sep=',')

如何自动执行此操作,而不是手动传递用户ID,而是自动从“用户”列中选择值并将其导出到相应的用户名文件。谢谢

3 个答案:

答案 0 :(得分:2)

使用带有for和f字符串的groupby循环(Python 3.6 +):

for user_id, group in user.groupby('User'):
    group.to_csv(f'user_{user_id}.csv', sep=',')

答案 1 :(得分:1)

您可以遍历User的唯一值:

import pandas as pd

user = pd.DataFrame({'User':['101','101','101','102','102','101','101','102','102','102'],'Country':['India','Japan','India','Brazil','Japan','UK','Austria','Japan','Singapore','UK'],'Count':[50,1,2,5,6,89,10.9,10,5,6]})

for u in user['User'].unique():
    file_name = 'user_{0}.csv'.format(u) 
    user[user['User'] == u].to_csv(file_name, sep=',') 

答案 2 :(得分:1)

您可以简单地循环unique值,并将df的子集放入csv文件中。

[user.loc[user.User==x].to_csv('user_'+x+'.csv',sep=',') for x in user.User.unique()]