Created
March 10, 2018 14:22
-
-
Save jrovegno/e10b0a8c74860239ad1f1c5a123d1d86 to your computer and use it in GitHub Desktop.
Append rows to DataFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
# Append correlative row from dict | |
df = pd.DataFrame(columns=['a','b','c']) | |
df = df.append(pd.Series({'a': 1, 'b': 2}), ignore_index=True) | |
df = df.append(pd.Series({'a': 4, 'c': 6}), ignore_index=True) | |
print('Append correlative row from dict\n', df) | |
# Append row from dict with defined index | |
df.loc[3] = pd.Series({'a': 1, 'b': 2, 'c': 3}) | |
#df = pd.concat([df, pd.DataFrame({'a': 1, 'b': 2, 'c': 3}, index=[3])]) | |
print('Append row from dict with defined index\n', df) | |
# Append row from dict with existing index | |
df = pd.concat([df, pd.DataFrame({'a': 4, 'b': 5, 'c': 6}, index=[1])]) | |
print('Append row from dict with existing index\n', df) | |
# Updating a pandas DataFrame row with a dictionary | |
df.loc[0] = pd.Series({'a': 7, 'c': 8}) | |
print('Updating a pandas DataFrame row with a dictionary\n', df) | |
# Remove duplicated index and keep the last value, then sort index | |
df = df[~df.index.duplicated(keep='last')].sort_index() | |
print('Remove duplicated index and keep the last value, then sort index\n', df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment