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
e.g. 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.
My long way where columns are id_names and rows are type_names:
Is it possible to append Series to rows of DataFrame without making a list first?
#!/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 dataframe Still getting an empty dataframe :/
I am trying to get the Series to be the rows, where the index of the Series becomes the column labels of the DataFrame
append() Pandas DataFrame. append() will append rows (add rows) of other DataFrame, Series, Dictionary or list of these to another DataFrame.
You can create a DataFrame and append a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to append dict as a row to DataFrame, not using this will get you an error.
By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function. The below example adds the list ["Hyperion",27000,"60days",2000] to the end of the pandas DataFrame. Yields below output.
Maybe an easier way would be to add the pandas.Series
into the pandas.DataFrame
with ignore_index=True
argument to DataFrame.append()
. Example -
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 -
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 -
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With