Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动从给定数据帧的numpy数组中的字符串创建新数据帧的名称_Python_Pandas - Fatal编程技术网

Python 自动从给定数据帧的numpy数组中的字符串创建新数据帧的名称

Python 自动从给定数据帧的numpy数组中的字符串创建新数据帧的名称,python,pandas,Python,Pandas,我有以下数据帧 col1 col2 col3 0 str9 47 55 1 str8 43 51 2 str9 46 52 3 str2 42 56 以及从df.col1.unique()生成的以下字符串数组 我想创建新的数据帧来管理我正在处理的数据量,其中

我有以下数据帧

      col1          col2          col3
0     str9          47            55
1     str8          43            51
2     str9          46            52
3     str2          42            56
以及从
df.col1.unique()生成的以下字符串数组

我想创建新的数据帧来管理我正在处理的数据量,其中每个新的数据帧表示
df[df.col1==strings[0]]
df[df.col1==strings[1]]
,等等
字符串中的所有值

我也想根据字符串中的值来命名它们,所以我们需要

df_str9 = df[df.col1 == strings[0]]
我知道我可以通过循环字符串来访问字符串中的每个值,但是如何创建数据帧,使其具有所列的名称要求

比如:

data_file = pd.DataFrame(data = ([['str9', 47, 55], ['str8',  43, 51], ['str9', 46, 52] , ['str2', 42, 56]] ), columns = (['col1', 'col2', 'col3']))
for string in strings:
    df_string = df[df.col1 == string]
您可能需要
locals()


这个问题相当广泛-尝试实现一些东西,然后回来问一些更具体的问题,而不是
“我该怎么做?”
你不能,最流行的解决方案似乎是将所有内容保存在字典中,并动态创建键。可能正是您想要的。啊,这太好了,我没有考虑将groupby作为解决方案,谢谢。啊,是的,我想我不应该在对问题的评论中说
不能动态创建变量名
。谢谢@Wen这是一个很好的解决方案。使用groupby建议制作词典的注释也可以。@dward4 groupby with dict是实现这种情况的方法:-)
data_file = pd.DataFrame(data = ([['str9', 47, 55], ['str8',  43, 51], ['str9', 46, 52] , ['str2', 42, 56]] ), columns = (['col1', 'col2', 'col3']))
for string in strings:
    df_string = df[df.col1 == string]
data_file = pd.DataFrame(data = ([['str9', 47, 55], ['str8',  43, 51], ['str9', 46, 52] , ['str2', 42, 56]] ), columns = (['col1', 'col2', 'col3']))

variables = locals()
for i in data_file['col1'].unique():
    variables["df_{0}".format(i)] = data_file.loc[data_file.col1 == i,]

print(df_str9)

print(df_str9)
   col1  col2  col3
0  str9    47    55
2  str9    46    52