Pandas各种方式读取dataframe的数据,有这篇就够了

######  loc只能通过index和columns的名称来取,不能用数字
########iloc只能用数字索引,不能用索引名
import numpy as np
from pandas import DataFrame
import pandas as pd
#构建dataframe,二维数组
df = DataFrame(np.arange(20).reshape(4,5),index=['one','two','three','four'],columns=list('abcde'))
print(df)

        a   b   c   d   e
one     0   1   2   3   4
two     5   6   7   8   9
three  10  11  12  13  14
four   15  16  17  18  19

############### 1.获取指定列的数据######
print("获取指定的某列:")
print(df['a'])
#注意双[]
print(df[['a','b']])

获取指定的某列:
one       0
two       5
three    10
four     15
Name: a, dtype: int32
        a   b
one     0   1
two     5   6
three  10  11
four   15  16

############### 2.获取指行的数据######
print("获取指行的某行:")
print(df.loc['one'])
print(df.iloc[0:1])

获取指行的某行:
a    0
b    1
c    2
d    3
e    4
Name: one, dtype: int32
     a  b  c  d  e
one  0  1  2  3  4

############### 3.loc获取指某行某列的数据######
######  loc只能通过index和columns的名称来取,不能用数字
print("loc获取指定某行某列的数据")
print(df.loc['one','c'])
#获取行索引one到three,列索引a到d的数据
print(df.loc['one':"three","a":'d'])
#获取行索引one到three,列索引a,d的数据
print(df.loc['one':'three',['a','d']])
#获取行索引one,three,列索引a,d的数据
print(df.loc[['one','three'],['a','d']])

loc获取指定某行某列的数据
2


        a   b   c   d
one     0   1   2   3
two     5   6   7   8
three  10  11  12  13


        a   d
one     0   3
two     5   8
three  10  13


        a   d
one     0   3
three  10  13

############### 4.iloc获取指某行某列的数据######
########iloc只能用数字索引,不能用索引名
print("iloc获取指定某行某列的数据")
print(df.iloc[0:1,2:3])
#获取行索引one到three,列索引a到d的数据
print(df.iloc[0:4,0:4])
#获取行索引one到three,列索引a,d的数据
print(df.iloc[0:4,[0,3]])
#获取行索引one,three,列索引a,d的数据
print(df.iloc[[0,2],[0,3]])

iloc获取指定某行某列的数据
     c
one  2


        a   b   c   d
one     0   1   2   3
two     5   6   7   8
three  10  11  12  13
four   15  16  17  18


        a   d
one     0   3
two     5   8
three  10  13
four   15  18


        a   d
one     0   3
three  10  13

############### 5.获取具体某个单值######
#iat取某个单值,只能数字索引
#at取某个单值,只能index和columns索引
print(df.iat[0,1])
print(df.at['one','b'])

1
1
 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在将 Pandas DataFrame 存储到 Hive 中之前,需要先将其转换为 Spark DataFrame。可以使用 PySpark 的 SQLContext 或 SparkSession 对象创建 Spark DataFrame。假设已经创建了一个名为 `pandas_df` 的 Pandas DataFrame,然后可以执行以下步骤将其存储到 Hive 中: 1. 导入必要的库和模块: ```python from pyspark.sql import SparkSession, SQLContext ``` 2. 创建 SparkSession 对象: ```python spark = SparkSession.builder \ .appName("pandas_to_hive") \ .config("spark.sql.warehouse.dir", "/user/hive/warehouse") \ .enableHiveSupport() \ .getOrCreate() ``` 其中,`appName` 是应用程序名称,`config` 指定了 Hive 数据仓库的路径,`enableHiveSupport` 用于启用 Hive 支持。 3. 将 Pandas DataFrame 转换为 Spark DataFrame: ```python spark_df = spark.createDataFrame(pandas_df) ``` 4. 将 Spark DataFrame 存储到 Hive 中: ```python spark_df.write \ .mode("overwrite") \ .saveAsTable("database_name.table_name") ``` 其中,`mode` 指定了写入模式,`saveAsTable` 将数据写入到指定的表中,如果表不存在,则会自动创建。 完整示例代码如下: ```python from pyspark.sql import SparkSession, SQLContext import pandas as pd # 创建 SparkSession 对象 spark = SparkSession.builder \ .appName("pandas_to_hive") \ .config("spark.sql.warehouse.dir", "/user/hive/warehouse") \ .enableHiveSupport() \ .getOrCreate() # 读取 Pandas DataFrame pandas_df = pd.read_csv("path/to/csv/file.csv") # 将 Pandas DataFrame 转换为 Spark DataFrame spark_df = spark.createDataFrame(pandas_df) # 将 Spark DataFrame 存储到 Hive 中 spark_df.write \ .mode("overwrite") \ .saveAsTable("database_name.table_name") ``` 请根据实际情况修改代码中的参数和路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值