Can pandas join tables if the common headers are different names? [duplicate] - python

This question already has answers here:
Joining pandas DataFrames by Column names
(3 answers)
Pandas Merging 101
(8 answers)
Closed last year.
I am following this article, but I was only able to get it to work by making sure there were matching titles, the two still had computer names, but they were called differently in the title, how could I modify my command so that it still references the same column, is that possible?
lj_df2 = pd.merge(d2, d3, on="PrimaryUser", how="left")
For example, I have this, but on my other csv, I have Employee # not primary user

Related

Python Merge duplicate rows based on ID and join with comma if column values are unique [duplicate]

This question already has answers here:
Pandas groupby with delimiter join
(2 answers)
Closed 8 months ago.
I have a CSV file called Project.csv
I am reading this file using pandas df = pd.read_csv('Project.csv',low_memory=False)
Inside this CSV file, there are rows with duplicate Project ID and Name but the other Column data are unique. I was looking for a way to find duplicate rows based on Project ID and merge records with ',' if they are unique.
Project.csv
I am looking to store this record in a data frame and filter it to make it look like this:
A simply groupby will do the job
after_df = your_df.groupby(['Project Id'])[['Project Name','Project Types','Owner']].agg(set)
This will give you a similar result to what you want. If you want to take out the key symbool of the strings parameters so you have a nice looking string do this.
after_df.astype(str).replace(r'{|}|\'','',regex=True)

Python - valuecounts() method - display all results [duplicate]

This question already has answers here:
How do I expand the output display to see more columns of a Pandas DataFrame?
(22 answers)
Closed 1 year ago.
I am a complete novice when it comes to Python so this might be badly explained.
I have a pandas dataframe with 2485 entries for years from 1960-2020. I want to know how many entries there are for each year, which I can easily get with the .value_counts() method. My issue is that when I print this, the output only shows me the top 5 and bottom 5 entries, rather than the number for every year. Is there a way to display all the value counts for all the years in the DataFrame?
Use pd.set_options and set display.max_rows to None:
>>> pd.set_option("display.max_rows", None)
Now you can display all rows of your dataframe.
Options and settings
pandas.set_option
If suppose the name of dataframe is 'df' then use
counts = df.year.value_counts()
counts.to_csv('name.csv',index=false)
As our terminal can't display entire columns they just display the top and bottom by collapsing the remaining values so try saving in a csv and see the records

Pandas inner join is not joining correctly [duplicate]

This question already has answers here:
Pandas Merging 101
(8 answers)
Closed 2 years ago.
I have two data frames where one looks like this and is called top_10_unique_artists:
and one that looks like this called artists:
I am trying to do an inner join based on the artistID by saying
import pandas as pd
top_10_unique_users.join(artists, on=top_10_unique_users.artistID)
however, when I do that the inner join is clearly not working properly because it is joining different ID's together rather than finding the artists in the artist table with the same ID as shown below:
You can use merge function, this way you can specify different column names in the two dataframes
import pandas as pd
pd.merge(top_10_unique_users,artists, how='left', left_on = 'artistID', right_on='id')
I cannot test the code as you only provided screenshots and not actual code but that should work.

Dropping rows based on specific values through python pandas? [duplicate]

This question already has answers here:
How do I select rows from a DataFrame based on column values?
(16 answers)
Closed 3 years ago.
This is the dataset that I am attempting to use:
https://storage.googleapis.com/hewwo/NCHS_-_Leading_Causes_of_Death__United_States.csv
I am wondering how I can specifically drop rows that contain certain values. In this example, many rows from the "Cause Name" column have values of "All causes". I want to drop any row that has this value for that column. This is what I have tried so far:
death2[death2['cause_name' ]!= 'All Causes']
While this did not give me any errors, it also did not seem to do anything to my dataset. Rows with "All causes" were still present. Am I doing something wrong?
No changes were made to your DataFrame. You need to reassign it if you want to change it.
death2 = death2[death2['cause_name' ]!= 'All Causes']

Difference in Pandas between df['myColumn'] and df.myColumn [duplicate]

This question already has answers here:
Pandas column creation
(3 answers)
Accessing Pandas column using squared brackets vs using a dot (like an attribute)
(5 answers)
pandas dataframe where clause with dot versus brackets column selection
(1 answer)
Closed 5 years ago.
I just thought I added a column to a pandas dataframe with
df.NewColumn = np.log1p(df.ExistingColumn)
but when I looked it wasn't there! No error was raised either.. I executed it many times not believing what I was seeing but yeah, it wasn't there. So then did this:
df['NewColumn'] = np.log1p(df.ExistingColumn)
and now the new column was there.
Does anyone know the reason for this confusing behaviour? I thought those two ways of operating on a column were equivalent..

Categories