在字典中加载 CSV 文件,然后为每个 csv 文件创建数据框 Python
我有多个 csv 文件 我可以使用关键字将它们作为数据帧加载到字典中。
# reading files into dataframes
csvDict = {}
for index, rows in keywords.iterrows():
eachKey = rows.Keyword
csvFile = "SRT21" + eachKey + ".csv"
csvDict[eachKey] = pd.read_csv(csvFile)
现在我有其他函数可以应用于每个数据帧的特定列。
在单个数据帧上,代码将如下所示
df['Cat_Frames'] = df['Cat_Frames'].apply(process)
df['Cat_Frames'] = df['Cat_Frames'].apply(cleandata)
df['Cat_Frames'] = df['Cat_Frames'].fillna(' ')
我的问题是如何循环遍历字典中的每个数据帧以应用这些函数?
我已经尝试过了
for item in csvDict.items():
df = pd.DataFrame(item)
df
,它给了我空结果,
有什么解决方案或建议吗?
I have multiple csv files
I was able to load them as data frames into dictionary by using keywords
# reading files into dataframes
csvDict = {}
for index, rows in keywords.iterrows():
eachKey = rows.Keyword
csvFile = "SRT21" + eachKey + ".csv"
csvDict[eachKey] = pd.read_csv(csvFile)
Now I have other functions to apply on every data frame's specific column.
on a single data frame the code would be like this
df['Cat_Frames'] = df['Cat_Frames'].apply(process)
df['Cat_Frames'] = df['Cat_Frames'].apply(cleandata)
df['Cat_Frames'] = df['Cat_Frames'].fillna(' ')
My question is how to loop through every data frame in the dictionary to apply those function?
I have tried
for item in csvDict.items():
df = pd.DataFrame(item)
df
and it gives me empty result
any solution or suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像这样链接
apply
:You can chain the
apply
s like this:Items 返回一个键/值的元组,所以你应该让你的 for 循环实际上说:
如果你不在 jupyter 中,你还需要打印 df
Items returns a tuple of key/value, so you should make your for loop actually say:
also you need to print the df if you aren't in jupyter
我认为这就是你应该如何遍历字典的方式。
I think this is how you should traverse the dictionary.
当没有处理涉及另一数据集的一个数据集/帧中的数据时,不要收集数据集。
只处理当前的一个,然后继续下一个。
接收不会被使用的“解压值”的变量的常规名称是
_
:- 但为什么要求键来忽略它们呢?迭代值:
When there is no processing of data from one data set/frame involving another data set, don't collect data sets.
Just process the current one, and proceed to the next.
The conventional name for a variable receiving an "unpacked value" not going to be used is
_
:- but why ask for keys to ignore them? Iterate the values: