pandas 如何将 Dataframe 〈class“str”>转换为 Dataframe

oalqel3c  于 2023-03-11  发布在  其他
关注(0)|答案(1)|浏览(257)

我有一个 Dataframe ,列“cc”包含 Dataframe 。但是我不能操作它,因为它是字符串。

Unnamed: 0   a    b                    cc
0           0  51  101 Unnamed: 0         ...
1           1  51  102 Unnamed: 0         ...
2           2  52  101 Unnamed: 0         ...
3           3  52  102 Unnamed: 0         ...

df.cc.iloc[0]显示了它包含的内容,但它是一个字符串:
提取 Dataframe 的方法是什么?我哪里错了?
a = df.cc.iloc[0] print(a.head())
属性错误:“str”对象没有属性“head”

a = df.cc.iloc[0] b = df.cc.iloc[0] c = pd.concat([a, b], ignore_index=True)
TypeError:无法串联类型〈class 'str'〉的对象;只有Series和DataFrame对象有效

bvk5enib

bvk5enib1#

如果选择列输出的第一个值是单值-这里是字符串。所以不能使用标量的Pandas函数。

df = pd.DataFrame({'cc':['a','b']})
                         
print (df.cc.iloc[0])
a

如果确实需要一个元素Series,则使用双[]

a = df.cc.iloc[[0]]
print (a)
0    a
Name: cc, dtype: object

一个元素DataFrame:

a = df[['cc']].iloc[[0]]
print (a)
  cc
0  a

由DataFrame构造函数生成:

a = df.cc.iloc[0]
print (a)
a

df1 = pd.DataFrame({'col':[a]})
print (df1)
  col
0   a

如果填充cc列的每个值完全新的DataFrame这不是最佳做法,但如果选择第一个值返回DataFrame:

def qwe(y,z): 
    df = pd.DataFrame({"one":range(1,10), "two":range(2,11), "three":range(3,12)}) 
    df["four"] = (df.one * (y+z)) + (df.two * (y+z)) + (df.three * (y+z)) 
    return df 

fr = pd.DataFrame(columns=["a", "b"]) 

for aa in range(1, 5): 
    for bb in range(1, 5): 
        x = [aa,bb] 
        fr.loc[len(fr)] = x
#for each value in cc is set DataFrame
fr["cc"] = fr.apply(lambda x: qwe(x['a'], x['b']), axis=1) 
print(fr.cc)
0        one  two  three  four
0    1    2      3   ...
1        one  two  three  four
0    1    2      3   ...
2        one  two  three  four
0    1    2      3   ...
3        one  two  three  four
0    1    2      3   ...
4        one  two  three  four
0    1    2      3   ...
5        one  two  three  four
0    1    2      3   ...
6        one  two  three  four
0    1    2      3   ...
7        one  two  three  four
0    1    2      3   ...
8        one  two  three  four
0    1    2      3   ...
9        one  two  three  four
0    1    2      3   ...
10       one  two  three  four
0    1    2      3   ...
11       one  two  three  four
0    1    2      3   ...
12       one  two  three  four
0    1    2      3   ...
13       one  two  three  four
0    1    2      3   ...
14       one  two  three  four
0    1    2      3   ...
15       one  two  three  four
0    1    2      3   ...
Name: cc, dtype: object
print(fr.cc.iloc[0])
   one  two  three  four
0    1    2      3    12
1    2    3      4    18
2    3    4      5    24
3    4    5      6    30
4    5    6      7    36
5    6    7      8    42
6    7    8      9    48
7    8    9     10    54
8    9   10     11    60

编辑:如果需要从fr.cc列加入每个DataFrame,请使用concat和列表解析:

out = pd.concat([x for x in fr.cc])
print(out)
    one  two  three  four
0     1    2      3    12
1     2    3      4    18
2     3    4      5    24
3     4    5      6    30
4     5    6      7    36
..  ...  ...    ...   ...
4     5    6      7   144
5     6    7      8   168
6     7    8      9   192
7     8    9     10   216
8     9   10     11   240

[144 rows x 4 columns]

如果混合cc列- Dataframe 与字符串:

out = pd.concat([x for x in fr.cc if isinstance(x, pd.DataFrame)])
print(out)
    one  two  three  four
0     1    2      3    12
1     2    3      4    18
2     3    4      5    24
3     4    5      6    30
4     5    6      7    36
..  ...  ...    ...   ...
4     5    6      7   144
5     6    7      8   168
6     7    8      9   192
7     8    9     10   216
8     9   10     11   240

[144 rows x 4 columns]

相关问题