Python 是否可以将 Series 附加到 DataFrame 的行而不先创建列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33094056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to append Series to rows of DataFrame without making a list first?
提问by O.rka
I have some data I'm trying to organize into a DataFrame
in Pandas
. I was trying to make each row a Series
and append it to the DataFrame
. I found a way to do it by appending the Series
to an empty list
and then converting the list
of Series
to a DataFrame
我有一些数据我试图组织成一个DataFrame
in Pandas
。我试图使每一行 aSeries
并将其附加到DataFrame
. 我找到了一种方法,通过将 the 附加Series
到一个空list
然后将list
of转换Series
为一个DataFrame
e.g. DF = DataFrame([series1,series2],columns=series1.index)
例如 DF = DataFrame([series1,series2],columns=series1.index)
This list
to DataFrame
step seems to be excessive. I've checked out a few examples on here but none of the Series
preserved the Index
labels from the Series
to use them as column labels.
这list
要DataFrame
一步似乎过度。我在这里查看了一些示例,但没有一个示例Series
保留了 中的Index
标签Series
以将它们用作列标签。
My long way where columns are id_names and rows are type_names:
我很长的路是列是 id_names 行是 type_names:
Is it possible to append Series to rows of DataFrame without making a list first?
是否可以将 Series 附加到 DataFrame 的行而不先创建列表?
#!/usr/bin/python
DF = DataFrame()
for sample,data in D_sample_data.items():
SR_row = pd.Series(data.D_key_value)
DF.append(SR_row)
DF.head()
TypeError: Can only append a Series if ignore_index=True or if the Series has a name
Then I tried
然后我试过了
DF = DataFrame()
for sample,data in D_sample_data.items():
SR_row = pd.Series(data.D_key_value,name=sample)
DF.append(SR_row)
DF.head()
Empty DataFrame
空数据帧
Tried Insert a row to pandas dataframeStill getting an empty dataframe :/
尝试向 Pandas 数据框插入一行仍然得到一个空的数据框:/
I am trying to get the Series to be the rows, where the index of the Series becomes the column labels of the DataFrame
我试图让系列成为行,其中系列的索引成为 DataFrame 的列标签
采纳答案by Anand S Kumar
Maybe an easier way would be to add the pandas.Series
into the pandas.DataFrame
with ignore_index=True
argument to DataFrame.append()
. Example -
也许更简单的方法是将 加入pandas.Series
到pandas.DataFrame
withignore_index=True
参数中DataFrame.append()
。例子 -
DF = DataFrame()
for sample,data in D_sample_data.items():
SR_row = pd.Series(data.D_key_value)
DF = DF.append(SR_row,ignore_index=True)
Demo -
演示 -
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([[1,2],[3,4]],columns=['A','B'])
In [3]: df
Out[3]:
A B
0 1 2
1 3 4
In [5]: s = pd.Series([5,6],index=['A','B'])
In [6]: s
Out[6]:
A 5
B 6
dtype: int64
In [36]: df.append(s,ignore_index=True)
Out[36]:
A B
0 1 2
1 3 4
2 5 6
Another issue in your code is that DataFrame.append()
is not in-place, it returns the appended dataframe, you would need to assign it back to your original dataframe for it to work. Example -
您的代码中的另一个问题DataFrame.append()
是未就地,它返回附加的数据帧,您需要将其分配回原始数据帧才能工作。例子 -
DF = DF.append(SR_row,ignore_index=True)
To preserve the labels, you can use your solution to include name for the series along with assigning the appended DataFrame back to DF
. Example -
为了保留标签,您可以使用您的解决方案来包含系列的名称,并将附加的 DataFrame 分配回DF
. 例子 -
DF = DataFrame()
for sample,data in D_sample_data.items():
SR_row = pd.Series(data.D_key_value,name=sample)
DF = DF.append(SR_row)
DF.head()
回答by BrenBarn
DataFrame.append
does not modify the DataFrame in place. You need to do df = df.append(...)
if you want to reassign it back to the original variable.
DataFrame.append
不会就地修改 DataFrame。df = df.append(...)
如果要将其重新分配回原始变量,则需要这样做。
回答by Selah
Something like this could work...
像这样的东西可以工作......
mydf.loc['newindex'] = myseries
Here is an example where I used it...
这是我使用它的一个例子......
stats = df[['bp_prob', 'ICD9_prob', 'meds_prob', 'regex_prob']].describe()
stats
Out[32]:
bp_prob ICD9_prob meds_prob regex_prob
count 171.000000 171.000000 171.000000 171.000000
mean 0.179946 0.059071 0.067020 0.126812
std 0.271546 0.142681 0.152560 0.207014
min 0.000000 0.000000 0.000000 0.000000
25% 0.000000 0.000000 0.000000 0.000000
50% 0.000000 0.000000 0.000000 0.013116
75% 0.309019 0.065248 0.066667 0.192954
max 1.000000 1.000000 1.000000 1.000000
medians = df[['bp_prob', 'ICD9_prob', 'meds_prob', 'regex_prob']].median()
stats.loc['median'] = medians
stats
Out[36]:
bp_prob ICD9_prob meds_prob regex_prob
count 171.000000 171.000000 171.000000 171.000000
mean 0.179946 0.059071 0.067020 0.126812
std 0.271546 0.142681 0.152560 0.207014
min 0.000000 0.000000 0.000000 0.000000
25% 0.000000 0.000000 0.000000 0.000000
50% 0.000000 0.000000 0.000000 0.013116
75% 0.309019 0.065248 0.066667 0.192954
max 1.000000 1.000000 1.000000 1.000000
median 0.000000 0.000000 0.000000 0.013116
回答by Vijay Ganesh Srinivasan
回答by tmldwn
Convert the series to a dataframe and transpose it, then append normally.
将系列转换为数据帧并转置,然后正常追加。
srs = srs.to_frame().T
df = df.append(srs)