是否可以在不先制作列表的情况下将 Series 附加到 DataFrame 的行?
Is it possible to append Series to rows of DataFrame without making a list first?
我有一些数据正在尝试整理到 Pandas
中的 DataFrame
中。我试图让每一行成为 Series
并将其附加到 DataFrame
。我找到了一种方法,将 Series
附加到空 list
,然后将 Series
的 list
转换为 DataFrame
例如DF = DataFrame([series1,series2],columns=series1.index)
list
到 DataFrame
这一步似乎过分了。我在这里查看了几个示例,但是 Series
中的 none 保留了 Series
中的 Index
标签以将它们用作列标签。
我的长路是 id_names 行 type_names:
是否可以在不先创建列表的情况下将 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
然后我试了
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()
空数据框
尝试过Insert a row to pandas dataframe
仍然得到一个空数据框:/
我试图让 Series 成为行,其中 Series 的索引成为 DataFrame 的列标签
也许更简单的方法是将 pandas.Series
添加到 pandas.DataFrame
中,并将 ignore_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)
演示 -
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
代码中的另一个问题是 DataFrame.append()
不在位,它 returns 附加的数据框,您需要将其分配回原始数据框才能正常工作。示例 -
DF = DF.append(SR_row,ignore_index=True)
要保留标签,您可以使用您的解决方案来包含系列的名称,并将附加的 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()
DataFrame.append
不会就地修改 DataFrame。如果要将其重新分配回原始变量,则需要执行 df = df.append(...)
。
像这样的东西可以工作...
mydf.loc['newindex'] = myseries
这是我使用它的例子...
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
尝试使用此命令。请参阅下面给出的示例:
df.loc[len(df)] = ['Product 9',99,9.99,8.88,1.11]
df
将系列转换为数据框并转置,然后正常追加。
srs = srs.to_frame().T
df = df.append(srs)
这也行得通:
df = pd.DataFrame()
new_line = pd.Series({'A2M': 4.059, 'A2ML1': 4.28}, name='HCC1419')
df = df.append(new_line, ignore_index=False)
Series 中的 name
将是数据帧中的索引。 ignore_index=False
是本例中的重要标志。
我有一些数据正在尝试整理到 Pandas
中的 DataFrame
中。我试图让每一行成为 Series
并将其附加到 DataFrame
。我找到了一种方法,将 Series
附加到空 list
,然后将 Series
的 list
转换为 DataFrame
例如DF = DataFrame([series1,series2],columns=series1.index)
list
到 DataFrame
这一步似乎过分了。我在这里查看了几个示例,但是 Series
中的 none 保留了 Series
中的 Index
标签以将它们用作列标签。
我的长路是 id_names 行 type_names:
是否可以在不先创建列表的情况下将 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
然后我试了
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()
空数据框
尝试过Insert a row to pandas dataframe 仍然得到一个空数据框:/
我试图让 Series 成为行,其中 Series 的索引成为 DataFrame 的列标签
也许更简单的方法是将 pandas.Series
添加到 pandas.DataFrame
中,并将 ignore_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)
演示 -
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
代码中的另一个问题是 DataFrame.append()
不在位,它 returns 附加的数据框,您需要将其分配回原始数据框才能正常工作。示例 -
DF = DF.append(SR_row,ignore_index=True)
要保留标签,您可以使用您的解决方案来包含系列的名称,并将附加的 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()
DataFrame.append
不会就地修改 DataFrame。如果要将其重新分配回原始变量,则需要执行 df = df.append(...)
。
像这样的东西可以工作...
mydf.loc['newindex'] = myseries
这是我使用它的例子...
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
尝试使用此命令。请参阅下面给出的示例:
df.loc[len(df)] = ['Product 9',99,9.99,8.88,1.11]
df
将系列转换为数据框并转置,然后正常追加。
srs = srs.to_frame().T
df = df.append(srs)
这也行得通:
df = pd.DataFrame()
new_line = pd.Series({'A2M': 4.059, 'A2ML1': 4.28}, name='HCC1419')
df = df.append(new_line, ignore_index=False)
Series 中的 name
将是数据帧中的索引。 ignore_index=False
是本例中的重要标志。