将 Pandas 系列转换为 DataFrame
- 2025-04-16 08:58:00
- admin 原创
- 22
问题描述:
我有一本熊猫系列科幻小说:
email
email1@email.com [1.0, 0.0, 0.0]
email2@email.com [2.0, 0.0, 0.0]
email3@email.com [1.0, 0.0, 0.0]
email4@email.com [4.0, 0.0, 0.0]
email5@email.com [1.0, 0.0, 3.0]
email6@email.com [1.0, 5.0, 0.0]
我想将其转换为以下 DataFrame:
index | email | list
_____________________________________________
0 | email1@email.com | [1.0, 0.0, 0.0]
1 | email2@email.com | [2.0, 0.0, 0.0]
2 | email3@email.com | [1.0, 0.0, 0.0]
3 | email4@email.com | [4.0, 0.0, 0.0]
4 | email5@email.com | [1.0, 0.0, 3.0]
5 | email6@email.com | [1.0, 5.0, 0.0]
我找到了一种方法来做到这一点,但我怀疑它是否更有效:
df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)
解决方案 1:
您无需创建 2 个临时 dfs,只需使用 DataFrame 构造函数将它们作为 dict 中的参数传递即可:
pd.DataFrame({'email':sf.index, 'list':sf.values})
有很多方法可以构造 df,请参阅文档
解决方案 2:
到帧():
从以下系列开始,email_series:
email
email1@email.com A
email2@email.com B
email3@email.com C
dtype: int64
email_series = pd.Series([" email1@email.com ", ...])
我使用to_frame将系列转换为 DataFrame:
df = email_series.to_frame().reset_index()
email 0
0 email1@email.com A
1 email2@email.com B
2 email3@email.com C
3 email4@email.com D
现在您需要做的就是重命名列名并命名索引列:
df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
您的 DataFrame 已准备好进行进一步分析。
更新:我刚刚看到这个链接,其中的答案与我的惊人地相似。
解决方案 3:
一行答案是
myseries.to_frame(name='my_column_name')
或者
myseries.reset_index(drop=True, inplace=True) # As needed
解决方案 4:
Series.reset_index
有name
争论
经常会出现需要将 Series 提升为 DataFrame 的情况。但如果 Series 没有名称,reset_index
则会导致类似这样的结果:
s = pd.Series([1, 2, 3], index=['a', 'b', 'c']).rename_axis('A')
s
A
a 1
b 2
c 3
dtype: int64
s.reset_index()
A 0
0 a 1
1 b 2
2 c 3
您看到的列名为“0”。我们可以通过指定name
参数来解决这个问题。
s.reset_index(name='B')
A B
0 a 1
1 b 2
2 c 3
s.reset_index(name='list')
A list
0 a 1
1 b 2
2 c 3
Series.to_frame
如果要创建 DataFrame 而不将索引提升到列,请按照此答案Series.to_frame
中的建议使用。它也支持 name 参数。
s.to_frame(name='B')
B
A
a 1
b 2
c 3
pd.DataFrame
构造函数
Series.to_frame
您还可以通过指定参数来执行相同的操作columns
:
pd.DataFrame(s, columns=['B'])
B
A
a 1
b 2
c 3
解决方案 5:
超级简单的方法也是
df = pd.DataFrame(series)
它将返回 1 列(系列值)+ 1 个索引(0....n)的 DF
解决方案 6:
Series.to_frame
可用于将 a 转换Series
为DataFrame
。
# The provided name (columnName) will substitute the series name
df = series.to_frame('columnName')
例如,
s = pd.Series(["a", "b", "c"], name="vals")
df = s.to_frame('newCol')
print(df)
newCol
0 a
1 b
2 c
解决方案 7:
可能被认为是一种非 Python 的方式来做到这一点,但这会在一行中给出你想要的结果:
new_df = pd.DataFrame(zip(email,list))
结果:
email list
0 email1@email.com [1.0, 0.0, 0.0]
1 email2@email.com [2.0, 0.0, 0.0]
2 email3@email.com [1.0, 0.0, 0.0]
3 email4@email.com [4.0, 0.0, 3.0]
4 email5@email.com [1.0, 5.0, 0.0]
解决方案 8:
series.to_frame() 方法和 pd.DataFrame() 方法用于将 Pandas Series 转换为 DataFrame。
import pandas
series = pandas.Series([40, 90, 80, 50, 70])
data_frame = series.to_frame()
print(data_frame)
或使用以下内容:
data_frame = pandas.DataFrame(series)
pd.concat() 方法用于在 Python 中将多个 Series 转换为单个 DataFrame。
data_frame = pandas.concat([series1, series2], axis=1)
解决方案 9:
只需使用reset_index()
您只需使用调用即可.reset_index()
将 Pandas 系列转换为 Pandas DataFrame。
df = series.reset_index()
这些列没有名称。要命名它们:
df.columns = ['col name 1', 'col name 2']
(假设有两列。)
扫码咨询,免费领取项目管理大礼包!