logo
down
shadow

Replace a whole dataframe with another (overwrite) (Python 3.4 pandas)


Replace a whole dataframe with another (overwrite) (Python 3.4 pandas)

By : Daniel Toussaint
Date : November 17 2020, 01:00 AM
like below fixes the issue Try Sframe = SframeWODup.copy() UPDATE: Can you use this code to achieve your desired outcome?
code :
# Made-up data
Sframe = pd.DataFrame({'TermName': ['Fall', 'Fall', 'Fall', 'Fall'], 
'DistrictName': ['Downtown', 'Downtown', 'Downtown', 'Downtown'], 
'SchoolName': ['Seattle Central', 'Ballard', 'Ballard', 'Ballard'], 
'StudentLastName': ['Doe', 'Doe', 'Doe', 'Doe'], 
'StudentFirstName': ['John', 'Jane', 'Jane', 'Jane'],
'StudentMI': ['X', 'X', 'X', 'X'],
'StudentID': ['1234', '9876', '9876', '9876'],
'StudentDateOfBirth': ['2000-01-01', '2001-01-01', '2001-01-01', '2001-01-01'],
'StudentEthnicGroup': ['Asian American', 'White', 'White', 'White'],
'StudentGender': ['M', 'F', 'F', 'F'],
'Grade': ['10th', '9th', '9th', '9th'],
'TermSchoolStudent': ['Z', 'Z', 'Z', 'Z']})

# Remove duplicates based upon StudentID, in-place (i.e., modify object 'Sframe'). 
# UPDATE: I read that you want duplicates completely removed from data frame.
# Sframe.drop_duplicates(cols = ['StudentID'], take_last = False, inplace = True)

Sframe['dup_check_1'] = Sframe.duplicated(cols = ['TermName', 'SchoolName', 'StudentID'], take_last = False)
Sframe['dup_check_2'] = Sframe.duplicated(cols = ['TermName', 'SchoolName', 'StudentID'], take_last = True)
Sframe = Sframe[(Sframe['dup_check_1'] == False) & (Sframe['dup_check_2'] == False)]
del Sframe['dup_check_1'], Sframe['dup_check_2']


Share : facebook icon twitter icon
How to replace&add the dataframe element by another dataframe in Python Pandas?

How to replace&add the dataframe element by another dataframe in Python Pandas?


By : Reuven Ivgi
Date : March 29 2020, 07:55 AM
I wish this help you Suppose I have two data frame 'df_a' & 'df_b' , both have the same index structure and columns, but some of the inside data elements are different: , Similar to BrenBarn's answer, but with more flexibility:
code :
# reindex both to union of indices
df_ar = df_a.reindex(df_a.index | df_b.index)
df_br = df_b.reindex(df_a.index | df_b.index)

# replacement criteria can be put in this lambda function
combiner = lambda: x, y: np.where(y < x, y, x)
df_c = df_ar.combine(df.br, combiner)
Python Pandas: how to overwrite subset of a dataframe with a subset of another dataframe?

Python Pandas: how to overwrite subset of a dataframe with a subset of another dataframe?


By : user3919583
Date : March 29 2020, 07:55 AM
will help you You are looking for the DataFrame.loc method
Small example:
code :
import pandas as pd
df1 = pd.DataFrame({"data":[1,2,3,4,5]})
df2 = pd.DataFrame({"data":[11,12,13,14,15]})
df3 = df1.copy()
df3.loc[3:4] = df2.loc[3:4]

df3
   data
0     1
1     2
2     3
3    14
4    15
Overwrite values in a dataframe with values from other dataframe in pandas python

Overwrite values in a dataframe with values from other dataframe in pandas python


By : bommuraj
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I have a two dataframes. One with a coordinates of sites, the other with with connections between them. For example: , Here's one way, create mapping from df1 and replace values in df2
code :
In [1120]: mapping = df1.set_index('SITE_ID').astype(str).apply(' '.join, axis=1).to_dict()

In [1121]: mapping
Out[1121]:
{101: '23.4244 31.5356',
 102: '45.009 14.2424',
 103: '35.3444 19.4242',
 104: '42.0 18.0002'}

In [1122]: df2.replace(mapping)
Out[1122]:
           SITE_ID            FIRST        SECOND  THIRD
0  23.4244 31.5356   45.009 14.2424  42.0 18.0002    NaN
1   45.009 14.2424  35.3444 19.4242           NaN    NaN
2  35.3444 19.4242     42.0 18.0002           NaN    NaN
3     42.0 18.0002              NaN           NaN    NaN
Python 3.x: Pandas DataFrame How to overwrite csv files (batch process)

Python 3.x: Pandas DataFrame How to overwrite csv files (batch process)


By : user2389366
Date : March 29 2020, 07:55 AM
wish of those help I think you could just not read the last column.
If your original file has 3 columns, then pd.read_csv('filepath', usecols=[0, 1]) should do the trick.
How to replace a word in dataframe by using another dataframe in Pandas python

How to replace a word in dataframe by using another dataframe in Pandas python


By : Jimmy Dunn
Date : March 29 2020, 07:55 AM
this one helps. Your solution should working if add words boundaries to Series.str.replace:
code :
nameList = '|'.join(r"\b{}\b".format(x) for x in allnames['name'])
df['string_data'] = df['string_data'].str.replace(nameList, "FirstName", case = False)
print (df)
   id                   string_data
0   1          My name is FirstName
1   2         Hello, I am FirstName
2   3  I like FirstName he is cool.
d = dict.fromkeys(allnames['name'], 'Firstname')
f = lambda x: ' '.join(d.get(y, y) for y in x.split())
df['string_data'] = df['string_data'].apply(f)
print (df)
   id                   string_data
0   1          My name is Firstname
1   2         Hello, I am Firstname
2   3  I like Firstname he is cool.
d = dict.fromkeys([x.lower() for x in allnames['name']], 'Firstname')
f = lambda x: ' '.join(d.get(y.lower(), y) for y in x.split())
df['string_data'] = df['string_data'].apply(f)
Related Posts Related Posts :
  • Testing logging output with pytest
  • How do I change my default working directory for Python (Anaconda) on VSCode?
  • .lower() for x in list, not working, but works in another scenario
  • Program gives error "List indices must not be string"
  • pyqt: Memory Usage
  • Confused about classes in Learn Python the Hard Way ex43?
  • Extracting unrecognized information from many CSV files
  • How do I connect to Postgresql server from Python?
  • Append rows to a pandas DataFrame without making a new copy
  • Scrapy: Importing a package from the project that's not in the same directory
  • launching Excel application using Python to view the CSV file , but CSV file is opening in read mode and cant view the d
  • Making a list in user-defined functions
  • Pyserial microcontroller to host communication
  • Plotting a line in between subplots
  • function not returning value. Error "NameError: name 'urlss' is not defined"
  • How to perform cartesian product with Tensorflow?
  • Multiple independent random number streams from single seed
  • I Need a simple and short python3 code that count secounds in a background process
  • No module named constants
  • from django 1.4 to django 1.5- argument 'verify_exists' what s replacement?
  • Slash replacement inside a raw string
  • Reordering columns/rows of a pivot_table?
  • MySQLdb.cursors.Cursor.execute does not work
  • Python module being reimported when imported at different places
  • Is the Session object from Python's Requests library thread safe?
  • Python Regex: Finding First and Last Names
  • Order by selection in List view of OpenERP 7.0
  • Reading input values in ipython notebook
  • List of dictionaries - how to read a specific value in a dictionary
  • writing os.system output to file
  • Create dictionary from points list and multiple attribute lists
  • How to write a table line by line with for loop
  • Map projection and forced interpolation
  • Django FBV's "render_to_response" equivalent in Class-Based-View?
  • Paramiko raises "SFTPError: Garbage packet received"
  • python pandas operations on columns
  • python list appending is not working
  • Speeding up matplotlib scatter plots
  • For each element of the list find closest date from a different list
  • How to prepend new rows at the beginning of an existing csv file?
  • how to make database robust to process kills with sqlite postgress and sqlalchemy?
  • finding a set of ranges that a number fall in
  • New line in python print() function
  • How to clear os.environ value for only one Variable in Python
  • Why does python append a modulo(%) operator to the end of a newly opened file?
  • recursive function or non-recursive that doesnt use 2 ** approach
  • Extract floats from a String using regular expression
  • How to solve: ImportError: No module named django.core.wsgi in Apache2 log?
  • What's the most concise way to include multiple statements in a Python switch case?
  • python xml pretty print not working
  • Can we use NumPy in writing a website?
  • Called bashscript doesn't start up GNU screen session
  • Reading fortran direct access data and writing formatted data - faster with python than with fortran?
  • Language supporting nested definition of functions
  • Find elements which exists in the most of the lists
  • Templating library in node.js similar to Jinja2 in Python?
  • Python Syntax Error, 'break' is outside the loop
  • Replacing spaces with hyphens
  • How to open a csv file in Microsoft Excel in Python?
  • ModuleNotFoundError: No module named 'users'
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org