英文:
How to export json from iteratively created dataframes in pandas
问题
我试图导出根据列值迭代创建的数据框。思路是使用列值来指定文件夹并筛选数据框。
为了迭代地创建数据框,我使用exec()。示例如下。想法是能够迭代运行创建df.to_json('dfName/'+datetime.today().strftime('%d-%m-%Y')+'.json'),其中dfName 会迭代更改为 a,b,c。如果这是一个重复的问题,我很抱歉,到目前为止我似乎没有找到类似的东西。
from datetime import datetime
import pandas as pd
data1 = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
data2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data3 = [10, 11, 12, 13, 14, 15, 16, 17, 18]
data = {
'Name': data1,
'data2': data2,
'data3': data3
}
df = pd.DataFrame(data)
for test in df.Name.unique():
exec(test + " = df[df['Name'] == test]")
请注意,我没有翻译代码部分,只提供了代码的中文注释。
英文:
I'm trying to export dataframes that are iteratively created based on the column value. The idea is that I would use both the column value to dictate the folder as well as filtering the dataframe.
In order to create the dataframes iteratively I'm using exec(). The example follows below. The idea would be to be able to run iteratively the creation of df.to_json('dfName/'+datetime.today().strftime('%d-%m-%Y')+'.json') where the dfName would change iteratively to a, b, c. I'm sorry if this is a duplicate I didn't seem to find anything of sorts so far
from datetime import datetime
import pandas as pd
data1 = ['a', 'a', 'a','b','b','b','c','c','c']
data2 = [1,2,3,4,5,6,7,8,9]
data3 = [10,11,12,13,14,15,16,17,18]
data = {
'Name':data1,
'data2':data2,
'data3':data3}
df = pd.DataFrame(data)
for test in df.Name.unique():
exec(test + "=df[df['Name'] == test]")
答案1
得分: 2
以下是代码部分的中文翻译,不包括代码注释:
# 你可以使用 `groupby()` 来进行无需过滤的操作:
from datetime import datetime
import pandas as pd
data1 = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
data2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
data3 = [10, 11, 12, 13, 14, 15, 16, 17, 18]
data = {
'Name': data1,
'data2': data2,
'data3': data3
}
df = pd.DataFrame(data)
for name, n_df in df.groupby('Name'):
# 进行你需要的操作... n_df.to_csv() 等等...
print(name)
print(n_df)
请注意,这是代码的中文翻译,没有包括代码的解释或其他信息。
英文:
You can do it without filters using groupby():
from datetime import datetime
import pandas as pd
data1 = ['a', 'a', 'a','b','b','b','c','c','c']
data2 = [1,2,3,4,5,6,7,8,9]
data3 = [10,11,12,13,14,15,16,17,18]
data = {
'Name':data1,
'data2':data2,
'data3':data3}
df = pd.DataFrame(data)
for name, n_df in df.groupby('Name'):
# do what you need... n_df.to_csv() etc...
print(name)
print(n_df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论