Объедините два столбца фрейма данных Pandas
Опубликовано: 27 Марта, 2022
Давайте обсудим, как объединить два столбца фрейма данных в pandas python. Мы можем сделать это, используя следующие функции:
- concat ()
- добавить ()
- присоединиться()
Example 1 : Using the concat() method.
# importing the moduleimport pandas as pd # creating 2 DataFrameslocation = pd.DataFrame({"area": ["new-york", "columbo", "mumbai"]})food = pd.DataFrame({"food": ["pizza", "crabs", "vada-paw"]}) # concatenating the DataFramesdet = pd.concat([location, food], join = "outer", axis = 1) # displaying the DataFrameprint(det) |
Output :
Example 2 : Using the append() method.
# importing the moduleimport pandas as pd # creating 2 DataFramesfirst = pd.DataFrame([["one", 1], ["three", 3]], columns =["name", "word"])second = pd.DataFrame([["two", 2], ["four", 4]], columns =["name", "word"]) # concatenating the DataFramesdt = first.append(second, ignore_index = True) # displaying the DataFrameprint(dt) |
Output :
Example 3 : Using the .join() method.
# importing the moduleimport pandas as pd # creating 2 DataFrameslocation = pd.DataFrame({"area" : ["new-york", "columbo", "mumbai"]})food = pd.DataFrame({"food": ["pizza", "crabs", "vada-paw"]}) # concatenating the DataFramesdt = location.join(food) # displaying the DataFrameprint(dt) |
Output :
For the three methods to concatenate two columns in a DataFrame, we can add different parameters to change the axis, sort, levels etc.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course