简体   繁体   English

我如何编写一个函数或循环,将每个唯一值分配到它自己的可访问数据框中?

[英]How could I write a function or loop that assigns each unique value into its own accessible dataframe?

I am looking for a way to create a dataframe for each unique value in a column of a dataframe.我正在寻找一种为数据框列中的每个唯一值创建数据框的方法。

I have a table like the following (obviously this is made up data):我有一个如下表(显然这是组成数据):

| Staff |  debit  |  credit  | Day      |
| Bob   |  100    |  25      | Monday   |
| Sue   |  15     |  95      | Wednesday|
| Bob   |  125    |  40      | Monday   |
| Bob   |  100    |  2       | Friday   |
| Sue   |  50     |  50      | Tuesday  |
| Bob   |  16     |  80      | Thursday |

I have created a dataframe dictionary that filters the way that I would like to using this code:我创建了一个数据框字典,用于过滤我希望使用此代码的方式:

x = dict(tuple(report.gropuby('Staff'))) x = dict(tuple(report.gropuby('Staff')))

Then I can call each key to print the filtered dataframe by:然后我可以通过以下方式调用每个键来打印过滤后的数据帧:

Bob = x['Bob']鲍勃 = x['鲍勃']

Which will look something like:这看起来像:

| Staff |  debit  |  credit  | Day      |
| Bob   |  100    |  25      | Monday   |
| Bob   |  125    |  40      | Monday   |
| Bob   |  100    |  2       | Friday   |
| Bob   |  16     |  80      | Thursday |

Is there a way that I can loop for each key in the dictionary to name and create a dataframe for each key from this dataframe dictionary?有没有一种方法可以为字典中的每个键循环命名并为此数据框字典中的每个键创建一个数据框? Then I would like to export each of the dataframes to its own Excel sheet (.xls or .xlsx) of the same Excel file.然后我想将每个数据框导出到同一个 Excel 文件的自己的 Excel 工作表(.xls 或 .xlsx)。 There are over 50 staff and it changes each fairly regularly so it would be nice to automate this rather than manually writing a list of names each time.有超过 50 名员工,而且每个人都定期更换,因此最好自动执行此操作,而不是每次手动编写姓名列表。

I figured a different approach that does what I wanted to do, maybe it will help someone else.我想出了一种不同的方法来做我想做的事情,也许它会帮助别人。 Instead of creating that dictionary and then trying to convert it to Excel, I instead converted into Excel using groupby from the original dataframe using the following code:我没有创建该字典然后尝试将其转换为 Excel,而是使用以下代码从原始数据帧中使用 groupby 转换为 Excel:

 with pd.ExcelWriter('test.xlsx') as writer: 
    for Staff, df in  df.groupby('Staff'): 
        df.to_excel(writer, sheet_name = Staff)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用熊猫,如何逐行遍历数据帧,但每一行都是其自己的数据帧 - Using pandas, how do I loop through a dataframe row by row but with each row being its own dataframe 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python? 在 Python 中,我如何构建一个循环,允许我读取 txt 文件(制表符分隔)并将每 1000 行存储为自己的数据帧? - In Python, how can I construct a loop that allows me read a txt file (tab delimited) and store each 1000 rows as its own dataframe? 如何对数据框列使用 pandas 的 df.get 函数,以便列中的每一行都保持自己的值? - How to use pandas' df.get function for a dataframe column so that each row in the column maintains its own value? 如何编写自定义函数来显示数据框中每个变量的值计数以及级别? - How do i write a custom function to show value counts of each variable in a dataframe along with levels? 如何将多键字典转换为 pandas dataframe,其中每个键和值都有自己的列? - How to convert a multi-key dictionary to a pandas dataframe, where each key and value has its own column? 如何使用for循环将列值添加到数据框字典中,以便每个数据框都获得一个唯一的列? - How to add a column value into dataframe dictionary using a for loop so that each dataframe gets a unique column? 如何将列表写入csv,并使列表中的每个元素成为其自己的列? - How do I write a list to a csv and make each element in the list be its own column? 如何制作 0 和 1 的 dataframe 使得每个唯一值都是一列? - How to make a dataframe of 0 and 1 such that each unique value is a column? 如何有效地将数据框中特定列中的行值与其自己的滞后值相乘 - How do I multiply a row value in a specific column in a dataframe with its own lagged value efficiently
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM