How do a join two columns into another seperate column in Pandas?

pandas merge columns into one
pandas combine two columns into list
pandas merge on different column names
pandas drop column
pandas rename column
pandas join vs merge
join two columns of dataframe pandas
pandas merge list of dataframes

Any help would be greatly appreciated. This is probably easy, but im new to Python. I want to add two columns which are Latitude and Longitude and put it into a column called Location.

For example:

First row in Latitude will have a value of 41.864073 and the first row of Longitude will have a value of -87.706819.

I would like the 'Locations' column to display 41.864073, -87.706819.

please and thank you.

df = pd.DataFrame(dict(lat=range(10, 20), lon=range(100, 110)))


This should be better than using apply

df.assign(location=[*zip(, df.lon)])

   lat  lon   location
0   10  100  (10, 100)
1   11  101  (11, 101)
2   12  102  (12, 102)
3   13  103  (13, 103)
4   14  104  (14, 104)
5   15  105  (15, 105)
6   16  106  (16, 106)
7   17  107  (17, 107)
8   18  108  (18, 108)
9   19  109  (19, 109)

list variant

Though I'd still suggest tuple

df.assign(location=df[['lat', 'lon']].values.tolist())

   lat  lon   location
0   10  100  [10, 100]
1   11  101  [11, 101]
2   12  102  [12, 102]
3   13  103  [13, 103]
4   14  104  [14, 104]
5   15  105  [15, 105]
6   16  106  [16, 106]
7   17  107  [17, 107]
8   18  108  [18, 108]
9   19  109  [19, 109]

How to Join Two Text Columns into a Single Column in Pandas , Let's see the different methods to join two text columns into a single column. Method #1: Using cat() function. We can also use different separators during join. How to Combine Two Columns in Pandas with + operator . Another way to join two columns in Pandas is to simply use the + symbol. For example, to concatenate First Name column and Last Name column, we can do. df["Name"] = df["First"] + df["Last"] We will get our results like this.

I question the usefulness of this column, but you can generate it by applying the tuple callable over the columns.

>>> df = pd.DataFrame([[1, 2], [3,4]], columns=['lon', 'lat'])
>>> df
   lon  lat
0    1    2
1    3    4
>>> df['Location'] = df.apply(tuple, axis=1)
>>> df
   lon  lat Location
0    1    2   (1, 2)
1    3    4   (3, 4)

If there are other columns than 'lon' and 'lat' in your dataframe, use

df['Location'] = df[['lon', 'lat']].apply(tuple, axis=1)

Join two text columns into a single column in Pandas, Note the index values on the other axes are still respected in the join. When gluing together multiple DataFrames, you have a choice of how to handle the other of DataFrame , the indexes must be disjoint but the columns do not need to be: The Series will be transformed to DataFrame with the column name as the  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more how to split 'number' to separate columns in pandas DataFrame

Data from Pir

   lat  lon        New
0   10  100  (10, 100)
1   11  101  (11, 101)
2   12  102  (12, 102)
3   13  103  (13, 103)
4   14  104  (14, 104)
5   15  105  (15, 105)
6   16  106  (16, 106)
7   17  107  (17, 107)
8   18  108  (18, 108)
9   19  109  (19, 109)

Merge, join, and concatenate, Used to merge the two dataframes column by columns. fill_valuescalar If True, columns in self that do not exist in other will be overwritten with NaNs. Returns. The first merge statement you perform should get you halfway there, but unfortunately, it's the second half of a two-step process. It sounds like you want to merge the sales data onto the visits data after summing the visits by Date/upc. You actually have to do a sum first (the merge command does not do this by itself). Try:

I definitely learned something from W-B and timgeb. My idea was to just convert to strings and concatenate. I posted my answer in case you wanted the result as a string. Otherwise it looks like the answers above are the way to go.

import pandas as pd
from pandas import *  

Dic = {'Lattitude': [41.864073], 'Longitude': [-87.706819]}
DF = pd.DataFrame.from_dict(Dic)
DF['Location'] = DF['Lattitude'].astype(str) + ',' +  DF['Longitude'].astype(str)

pandas.DataFrame.combine, Note the index values on the other axes are still respected in the join. join_axes In the case of DataFrame, the indexes must be disjoint but the columns do not need to be: append may take multiple objects to concatenate: The Series will be transformed to DataFrames with the column name as the name of the Series. Often you may have a column in your pandas data frame and you may want to split the column and make it into two columns in the data frame. For example, one of the columns in your data frame is full name and you may want to split into first name and last name (like the figure shown below).

Merge, join, and concatenate, Combine data from multiple files into a single DataFrame using merge and concat. We can use the concat function in pandas to append either columns or rows from It will automatically detect whether the column names are the same and will We can save it to a different folder by adding the foldername and a slash to  Let’s see how to split a text column into two columns in Pandas DataFrame. Method #1 : Using Series.str.split() functions. Split Name column into two different columns. By default splitting is done on the basis of single space by str.split() function.

Combining DataFrames with Pandas – Data Analysis and , Pandas merge(): Combining Data on Common Columns or Indices Part of their power comes from a multifaceted approach to combining separate datasets. If you do not specify the merge column(s) with on , then Pandas will use any With the two datasets loaded into DataFrame objects, you'll select a  In this short guide, I’ll show you how to concatenate column values in pandas DataFrame. To start, you may use this template to concatenate your column values (for strings only): df1 = df['1st Column Name'] + df['2nd Column Name'] + Notice that the plus symbol (‘+’) is used to perform the concatenation.

Combining Data in Pandas With merge(), .join(), and concat() – Real , Concatenate DataFrames along row and column. Merge DataFrames on specific keys by different join logics like left-join, To transform this into a pandas DataFrame, you will use the DataFrame() function of pandas, along with its columns Similar to the previous DataFrame df1 , you will create two more  Concatenate two columns of dataframe in pandas python Concatenating two columns of the dataframe in pandas can be easily achieved by using simple ‘+’ operator. Concatenate or join of two string column in pandas python is accomplished by cat() function. we can also concatenate or join numeric and string column.

  • Hey BIll. It looks like you got a bunch of answers. If the problem is solved, we would appreciate if you selected one of the answers as accepted. Thanks!
  • "This should be better than using apply" <- Can you please explain a bit more? Personally, I think apply is easier to read, is there a performance issue or something else I am missing?
  • apply is great particularly for the reason that it is very intuitive. Therefore, if performance isn't an issue, it is a fine solution. However, it is known that apply can have performance issues. I try to avoid using a performance bludgeon for every question and answer but I do try to avoid using apply in my answers. But I will admit that in some cases, it makes the answers more elegant. Personally, I like zip. It is a core component of the language and deserves understanding so that it too becomes "readable".
  • Sorry should have mentioned that I have other columns as well. When I tried what you suggested it added the other columns into location also. Anyway to specifically join lon & lat?
  • @BillCarson see the updated answer.
  • Thanks for sharing.
  • OP does not state that they need to create a new dataframe. I would not recommend the method you present. You're creating 2 dictionaries then 2 dataframes only to join them together. This is unnecessary. However, none of the other answers mention actually joining as a string. That is a valid interpretation but you are doing a lot of converting of types for very little benefit. I'd recommend sticking with a tuple.
  • I see what you're saying piRSquared. The creation of the 2 dictionaries and 2 dataframes was to get to the point where I thought the OP was starting from. I updated my post to create 1 dictionary and 1 dataframe. I agree that your solution is much more elegant...just taking a stab at it with the arsenal I've learned up to this point lol.