[英]How can I print all data for each unique observation to its own PDF or CSV using R or Python?
Consider the following example dataframe. How can I group by Name and then write all information pertaining to each unique observation under the name column to a PDF, CSV, or Excel file?考虑以下示例 dataframe。如何按名称分组,然后将与名称列下每个唯一观察值相关的所有信息写入 PDF、CSV 或 Excel 文件? For example I would like all of Dave's information printed to a file named "Dave", all of Sal's information printed to a file named "Sal".
例如,我希望将 Dave 的所有信息打印到名为“Dave”的文件中,将 Sal 的所有信息打印到名为“Sal”的文件中。
Name | Score| Date | Test |
Dave | 95 | 09/03/21 | Math |
Dave | 90 | 09/20/21 | History |
Sal | 85 | 09/18/21 | Math |
Jackie| 89 | NA | English |
Sal | 88 | 09/15/21 | Gym |
Goat | 18 | 09/17/21 | Gym |
Jackie| 82 | 10/16/21 | Art |
Goat | 3 | 10/17/21 | Math |
Ty | 25 | 09/28/21 | Math |
Cheers干杯
In R:在 R 中:
names <- unique(df$Name)
for(nam in names){
write.csv(x = df[df$Name== nam,], file = paste0(nam, ".csv"))
}
You can just put everything in a loop:您可以将所有内容放在一个循环中:
for name in my_df['Name'].unique().tolist():
new_df = my_df[my_df.Name ==name]
file_path_name = 'your path' + name +'.csv'
new_df.to_csv(file_path_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.