简体   繁体   English

如何使用 R 或 Python 将每个独特观察的所有数据打印到其自己的 PDF 或 CSV?

[英]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.

相关问题 使用Python,如何遍历一个csv文件并打印每个单元格? - Using Python, how can I loop through a csv file and print each cell? 当使用 popen 运行或在子进程库中运行时,如何在自己的行上打印 python 中的输入提示? - How can I print an input prompt in python on its own line when ran using popen or run in the subprocess library? 如何在 python 中每个“父”行的末尾打印我的所有 json 嵌套数据? - How can I print ALL my json nested data in the end of each "father" line in python? 如何使用 for 循环打印没有每个项目的所有列表,python 中是否有替代方案? - How can I print all lists without each item using for loops, and is there an alternative in python? 如何使用 python 打印 a.pdf 文件中的表格 - How can I print the tables in a .pdf file using python 如何将数据框列中的每个值移到其自己的列中? - How can I move each value in a data frame column into its own column? 如何使用 Python 将数据打印到 CSV - How to data print data to CSV using Python 如何查询我已输入的csv文件的特定列并使用python打印所有返回的行? - How can i query a specific column of a csv file that i have input and print all returned rows using python? 使用R或Matplotlib(Python),如何基于CSV文件每一行的值比较来创建维恩图? - Using R or Matplotlib (Python), how can I create a venn diagram based on value comparisons for each row of a CSV file? Python:将每个 excel 行打印为自己的 ms word 页面 - Python: print each excel row as its own ms word page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM